I have a shapefile covering the whole globe, in EPSG:4326 (geodetic WGS84). I would need to compute the polygons areas in a metric such as km2.
How can I do that in QGIS or with GDAL/OGR and having a good precision?
I would first convert it to an equal area projection and then do the field calculator for area
you can not calculate an area with a geographic coordinate system such as 4326
find a projected coordinate system that suits your needs. any equal area world projection would work. good list of projections/coordinate system: http://spatialreference.org/
If you don't want to use an equal-area reprojection, you might get acceptable accuracy by calculating an approximation with the field calculator. It will calculate polygon areas as DecimalDegrees^2 with the $area field, but you would need to correct it for the decreasing scale of the longitudinal measurements. Entering an expression of:
Will convert to km^2, assuming that ymin is a reasonable estimate of latitude, that the polygons are roughly the same height and width in decimal degrees, constant earth radius, etc....
You could improve on these assumptions somewhat by averaging ymin and ymax to get latitude in the middle, and maybe by taking into account the ratio of the longitudinal width (xmax-xmin) to the latitudinal height (ymax-ymin), but re-projecting to an equal area projection before using $area would be more correct.
I would import a shapefile of UTM zones such as this one and Join by location the UTM zone in which your polygons fall in. Then match these zone with their respective EPSG. e.g.: UTM zone 10 N = EPSG:32610. Once that is done you can calculate more precisely the Area by transforming the projection in your equation like so:
area(transform( $geometry , 'EPSG:4326', 'your EPSG attribute'))