cURL link error with MinGW-w64 GCC on MSYS2

2.4k views Asked by At

I am using MSYS2, and MinGW-w64 originated libraries and toolchains (from pacman repository 'mingw32') via mingw32_shell.bat environment.

I use mingw32/mingw-w64-i686-gcc package; mingw32 packaged are path prefixed /mingw32, so the gcc binary is /mingw32/gcc, for example.

What I am in trouble with is, linking cURL libraries with g++. I installed mingw32/mingw-w64-i686-curl. It provides /mingw32/include/curl/curl.h, /mingw32/lib/libcurl.a, /mingw32/lib/libcurl.dll.a, and so on. While this package does not provide curl.pc, -lcurl does not work. So I run:

$ curl-config --cflags
-I/mingw32/include
$ curl-config --libs
-L/mingw32/lib -lcurl -lidn -lrtmp -lssh2 -lssl -lcrypto -lgdi32 -lssl -lcrypto -lwldap32 -lz -lws2_32
$ curl-config --static-libs
/mingw32/lib/libcurl.a -LC:/msys64/mingw32/lib -pipe -LC:/msys64/mingw32/lib -lidn -lrtmp -lssh2 -lssl -lcrypto -lgdi32 -lssl -lcrypto -lwldap32 -lz -lws2_32

Good. I added -I/mingw32/include to CXXFLAGS in my Makefile, and added /mingw32/lib/libcurl.dll.a to g++ arguments in the executable linking target in the Makefile. (The CXXFLAGS contains -std=c++11 -fopenmp -O2 -g -I..)

It failed.

main.o: In function `query':
C:\Users\Administrator\ngd\5/main.cc:80: undefined reference to `_imp__curl_easy_init'
main.o: In function `urlencode':
C:\Users\Administrator\ngd\5/main.cc:34: undefined reference to `_imp__curl_free'
C:\Users\Administrator\ngd\5/main.cc:29: undefined reference to `_imp__curl_easy_escape'
C:\Users\Administrator\ngd\5/main.cc:30: undefined reference to `_imp__curl_easy_escape'
main.o: In function `query':
C:\Users\Administrator\ngd\5/main.cc:88: undefined reference to `_imp__curl_easy_setopt'
C:\Users\Administrator\ngd\5/main.cc:93: undefined reference to `_imp__curl_easy_perform'
C:\Users\Administrator\ngd\5/main.cc:94: undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status

(main.o is from main.cc, which includes curl/curl.h and curl function usages.)

I added -DCURL_STATICLIBS -I/mingw32/include to CXXFLAGS instead, and /mingw32/lib/libcurl.a to g++ arguments for an executable.

Failed again.

main.o: In function `query':
C:\Users\Administrator\ngd\5/main.cc:80: undefined reference to `curl_easy_init'
main.o: In function `urlencode':
C:\Users\Administrator\ngd\5/main.cc:34: undefined reference to `curl_free'
C:\Users\Administrator\ngd\5/main.cc:35: undefined reference to `curl_free'
C:\Users\Administrator\ngd\5/main.cc:29: undefined reference to `curl_easy_escape'
C:\Users\Administrator\ngd\5/main.cc:30: undefined reference to `curl_easy_escape'
main.o: In function `query':
C:\Users\Administrator\ngd\5/main.cc:88: undefined reference to `curl_easy_setopt'
C:\Users\Administrator\ngd\5/main.cc:90: undefined reference to `curl_easy_setopt'
C:\Users\Administrator\ngd\5/main.cc:91: undefined reference to `curl_easy_setopt'
C:\Users\Administrator\ngd\5/main.cc:93: undefined reference to `curl_easy_perform'
C:\Users\Administrator\ngd\5/main.cc:94: undefined reference to `curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status

What I did to examine:

$ nm /mingw32/lib/libcurl.a
... (other files)
libcurl_la-escape.o:
...
00000340 T _curl_free
...
(end of libcurl_la-escape.o)
... (other files)

$ nm /mingw32/lib/libcurl.dll.a
... (other files)
d000018.o:
...
00000000 I __imp__curl_free
00000000 T _curl_free
...
(end of d000018.o)
... (other files)

So the problem is (I think) __imp__curl_free or _curl_free is defined, while the compiler requires _imp__curl_free or curl_free.

Any solution for this please?

0

There are 0 answers