curl / Mailing Lists / curl-library / Single Mail


Re: Is it safe to call `curl_global_init` before main()?

From: Victor Abrash <>
Date: Mon, 17 Apr 2017 11:05:13 -0700

On 4/16/17 2:42 AM, Daniel Stenberg wrote:
> On Wed, 12 Apr 2017, wrote:
>> Is it okay to call `curl_global_init` and `curl_global_cleanup`
>> before and after main() respectively?
> Yes it probably is. libcurl has no relation to main() so it doesn't
> care if it gets called before or after that entry point. As long as
> the heap is ready to get used etc.
> The main thing the curl_global_init function does is setting up third
> party libraries so they are really the ones that need to work in this
> situation.
I can confirm that this works.

Best approach in C++ is to put the calls in the constructor/destructor
of a class, with a single static instantiation. This guarantees that
the two Curl functions are only called once per process. You can
initialize other libraries in the same methods. Keep things simple and
don't put any other methods into the class.

This works (at least) on Linux, Windows, and MacOS.

Received on 2017-04-17