Search code examples
cwindowscurlmingwstatic-libraries

using static libcurl.a in C program by minGW64


(I saw answers of every single StOF questions regarding this - none fully helped. I'm very frustrated after trying so hard for 3 days & nights.)


  • libcurl.a is statically linked with:
    • OpenSSL 1.1.1k [64bit/32bit]
    • brotli 1.0.9 [64bit/32bit]
    • libgsasl 1.10.0 [64bit/32bit]
    • libidn2 2.3.1 [64bit/32bit]
    • libssh2 1.9.0 [64bit/32bit]
    • nghttp2 1.43.0 [64bit/32bit]
    • zlib 1.2.11 [64bit/32bit]
    • zstd 1.5.0 [64bit/32bit]

Case 1 - as if curl isn't statically linked

x86_64-w64-mingw32-gcc-10.2.0.exe -o main.exe main.c "C:\curl-7.77.0-win64-mingw\lib\libcurl.a" -DCURL_STATICLIB

Throws unending lines of error, as if libcurl isn't statically linked with its dependencies*:

...\lib\libcurl.a(http2.o):(.text+0x7f): undefined reference to `nghttp2_version'
...\lib\libcurl.a(http2.o):(.text+0x297): undefined reference to `nghttp2_submit_rst_stream' 

... (then the errors include many more undefined reference to symbols from nghttp2, ssl, crypt, ssh, gsasl)

Solution

  • It is so enormously saddening to see how many people have struggled and are still struggling to statically link libcurl to their program. So much so that a very active Curl maintainer said: "building static is a roller coaster left for the users to deal with on their own as its such a never-ending race for us to try to support."


    Since linker is saying undefined references, then libcurl.a must be:

    • not statically linked
    • Or, you've got the sequence of libraries unarranged. Linker is sensitive to sequence. Example: If libbrotlidec-static.a needs a function/symbol which is inside libbrotlienc-static.a, then libbrotlienc-static.a must be mentioned before libbrotlidec-static.a

    A static library is an archive .a of object .obj files. And they're not statically linked in themselves. That's why, to link some-static-library.a to a program, you need to collect and manually mention every.a single.a static.a library.a that are dependencies of some-static-library.a.


    In my Chat@Terminal:~$ project, I should have a make.bat file which shows how to statically link libcurl to a program using gcc or mingw. And finally static-compile the whole program, and ship without any runtime dependency!


    On a side-note, curl's precompiled-binary website says: Curl_x.x.x is statically linked with: [list of libraries you provided]. Break your misconception that, the statement made at the website means: Curl.exe is statically linked with the libs, not libcurl.