{"id":"0c393ec60cce7464","repo":"curl/curl","slug":"could-not-init-curl-0c393e","errorCode":null,"errorMessage":"Could not init curl\n","messagePattern":"Could not init curl\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"docs/examples/url2file.c","lineNumber":59,"sourceCode":"  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);\n  return written;\n}\n\nint main(int argc, const char *argv[])\n{\n  static const char pagefilename[] = \"page.out\";\n\n  CURLcode result;\n  CURL *curl;\n\n  if(argc < 2) {\n    printf(\"Usage: %s <URL>\\n\", argv[0]);\n    return 1;\n  }\n\n  result = curl_global_init(CURL_GLOBAL_ALL);\n  if(result != CURLE_OK) {\n    fprintf(stderr, \"Could not init curl\\n\");\n    return (int)result;\n  }\n\n  /* init the curl session */\n  curl = curl_easy_init();\n  if(curl) {\n    FILE *pagefile;\n\n    /* set URL to get here */\n    curl_easy_setopt(curl, CURLOPT_URL, argv[1]);\n\n    /* Switch on full protocol/debug output while testing */\n    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);\n\n    /* disable progress meter, set to 0L to enable it */\n    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);\n\n    /* send all data to this function */","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/curl/curl/blob/527573490eb2564b3d7c9dd51d8bff963b5d6303/docs/examples/url2file.c#L41-L77","documentation":"Printed by the url2file.c example when curl_global_init(CURL_GLOBAL_ALL) returns a value other than CURLE_OK. Unlike most examples that return the code silently, this one explicitly reports that global libcurl initialization failed before any easy handle is created. The %s placeholder is intentionally absent (no strerror used here), so it is a coarse-grained failure indicator.","triggerScenarios":"curl_global_init(CURL_GLOBAL_ALL) fails at process startup, which can occur when libcurl cannot initialize a subsystem (e.g. SSL backend) or when the function is called in an environment where the loaded libcurl is broken/incompatible.","commonSituations":"Linking against a libcurl whose required TLS backend DLL/shared library is missing or version-mismatched; calling the function after another part of the program already initialized or cleaned up libcurl; a corrupted/partial libcurl install; ABI mismatch between headers used to compile and the shared library present at runtime.","solutions":["Check the curl_global_init return code and print curl_easy_strerror(result) to identify the failing subsystem","Ensure the libcurl shared library and its TLS backend (OpenSSL/Schannel/etc.) are present and version-matched at runtime (ldd / ldd-path on the binary)","Make sure curl_global_init is called exactly once before any easy handle and curl_global_cleanup only at shutdown","Reinstall/rebuild libcurl consistently (matching headers and library) if an ABI mismatch is suspected"],"exampleFix":"// before\nresult = curl_global_init(CURL_GLOBAL_ALL);\nif(result != CURLE_OK) {\n  fprintf(stderr, \"Could not init curl\\n\");\n  return (int)result;\n}\n\n// after: report the actual libcurl error for diagnosis\nresult = curl_global_init(CURL_GLOBAL_ALL);\nif(result != CURLE_OK) {\n  fprintf(stderr, \"Could not init curl: %s\\n\", curl_easy_strerror(result));\n  return (int)result;\n}","handlingStrategy":"try-catch","validationCode":"/* Guarantee curl_global_init() ran exactly once before easy_init(). */\nstatic int g_inited = 0;\nstatic int ensure_curl_init(void) {\n  if (g_inited) return 1;\n  if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) return 0;\n  g_inited = 1;\n  atexit(curl_global_cleanup);\n  return 1;\n}\nif (!ensure_curl_init()) {\n  fprintf(stderr, \"curl_global_init failed; cannot init curl\\n\");\n  exit(EXIT_FAILURE);\n}\nCURL *c = curl_easy_init();\nif (!c) { fprintf(stderr, \"Could not init curl\\n\"); exit(EXIT_FAILURE); }","typeGuard":"/* Carry the non-null easy handle through a typed pointer so NULL is impossible downstream. */\ntypedef struct { CURL *easy; int valid; } easy_handle_t;\nstatic easy_handle_t make_easy(void) {\n  easy_handle_t h = { NULL, 0 };\n  if (ensure_curl_init()) { h.easy = curl_easy_init(); h.valid = (h.easy != NULL); }\n  return h;\n}","tryCatchPattern":"easy_handle_t h = make_easy();\nif (!h.valid) {\n  fprintf(stderr, \"Could not init curl\\n\");\n  return EXIT_FAILURE;          /* nothing to cleanup: easy_init itself failed */\n}\n/* ... use h.easy ... */\ncurl_easy_cleanup(h.easy);","preventionTips":["Call curl_global_init() exactly once at program start (it is NOT thread-safe and not idempotent); a missing or failed global init is the usual cause of curl_easy_init() returning NULL.","Do not call any libcurl function before global init in multi-threaded code — initialize in main() before spawning threads.","Check the return of curl_global_init() before calling curl_easy_init(); treat NULL from easy_init as fatal, not as a retryable network error.","If you link libcurl statically, confirm the TLS backend was initialized at build; a backend mismatch can make init fail."],"tags":["curl","initialization","libcurl","startup","tls"],"analyzedSha":"527573490eb2564b3d7c9dd51d8bff963b5d6303","analyzedAt":"2026-08-01T21:00:26.351Z","schemaVersion":2}