Cannot link against GLFW 3 with MinGW-w64 - c++

Compiling code that uses GLFW in MinGW-w64, everything goes fine until the linking phase. At this point, ld starts to complain about undefined references to GLFW functions, even though I specified the additional library directories and used libraries. Here's my g++ invocation (I, of course, use a lot of enviroment variables in my batch file to shorten the command, here it is unwrapped):
x86_64-w64-mingw32-g++.exe -I./deps/glfw-3.2.1/include -I./deps/gl3w/include -I./include -L./deps/glfw-3.2.1/lib-mingw-w64/x86_64 -lglfw3 -lopengl32 -lgdi32 -lkernel32 -luser32 -lcomdlg32 -std=c++14 -fpermissive -mwindows -o build\out\windows\Release\x86_64\GemVerse.exe ./deps/gl3w/src/gl3w.c ./src/main.cpp
Here is the same command, broken down:
x86_64-w64-mingw32-g++.exe %INCLUDEDIRS% %LIBDIRS64% %CFLAGS% %LIBS% -o build\out\windows\Release\x86_64\GemVerse.exe %SOURCES%
Where %INCLUDEDIRS%=-I./deps/glfw-3.2.1/include -I./deps/gl3w/include -I./deps/glm-0.9.9.3/include -I./include, %LIBDIRS64%=-L./deps/glfw-3.2.1/lib-mingw-w64/x86_64, %LIBS%=-lglfw3 -lopengl32 -lgdi32 -lkernel32 -luser32 -lcomdlg32, %CFLAGS%=-std=c++14 -fpermissive -mwindows and %SOURCES%=./deps/gl3w/src/gl3w.c ./src/main.cpp
As you can see, the library gets found and linked, but the symbols are still unresolved. How is this possible and how to link GLFW properly?
EDIT 1:I've changed the library order, now it's -lopengl32 -lgdi32 -lkernel32 -luser32 -lcomdlg32 -glfw3, so that GLFW is at the end. That didn't fix the problem, ld still doesn't find the definitions. Here is the g++ log, since I don't seem to have attached one, my fault:
./deps/gl3w/src/gl3w.c: In function 'int open_libgl()':
./deps/gl3w/src/gl3w.c:47:52: warning: invalid conversion from 'FARPROC' {aka 'long long int (*)()'} to 'void*' [-fpermissive]
*(void **)(&wgl_get_proc_address) = GetProcAddress(libgl, "wglGetProcAddress");
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x65): undefined reference to `glfwInit'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x74): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x83): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x92): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0x9e): undefined reference to `glfwWindowHint'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0xc0): undefined reference to `glfwCreateWindow'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text$_ZN8gemverse6GVGame4initEv[_ZN8gemverse6GVGame4initEv]+0xd4): undefined reference to `glfwMakeContextCurrent'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x1b): undefined reference to `glfwTerminate'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x3b): undefined reference to `glfwGetKey'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x5c): undefined reference to `glfwSwapBuffers'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x61): undefined reference to `glfwPollEvents'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x66): undefined reference to `glfwTerminate'
C:\Users\vtonc\AppData\Local\Temp\cc6GSYa1.o:main.cpp:(.text.startup+0x7d): undefined reference to `glfwSetWindowShouldClose'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
Some names are mangled, since the initialization code in my project is incapsulated into a class.

I fixed the problem by putting %SOURCES% before %LIBS% in the g++ invocation, thanks to this answer. That seems to make difference as well: not only the library order is important, but the order of inputs (sources before libraries) too.

Related

Statically link SDL on Windows (MinGW) / undefined references

I built SDL (2.0.10) using cmake and now I am trying to link it to my application.
For linking I use g++ $(OBJS) -L./Path/To/SDL/LibFolder -lmingw32 -lSDL2main -lSDL2-static -oOut.exe
When I link it dynamically using SDL2, it works without problems. But I would like to link it statically.
I tried including every library that I found in error log (-lkernel32 -ladvapi32 -lgdi32 -limm32 -lmsvcrt -lole32 -loleaut32 -lsetupapi -lshell32 -luser32 -lversion -lwinmm -lmingw32 -lSDL2main -lSDL2-static), but it did nothing. I guess I don't understand how this linking stuff works.
It outputs this: (There is more, but I want to keep it simple. There are more undefined references to libraries that I listed above.)
./deps/windows/sdl/lib/libSDL2-static.a(SDL_windows.c.obj): In function `WIN_CoInitialize':
D:/Users/alesg/Desktop/comp/SDL-mirror/src/core/windows/SDL_windows.c:76: undefined reference to `__imp_CoInitializeEx'
./deps/windows/sdl/lib/libSDL2-static.a(SDL_windows.c.obj): In function `WIN_CoUninitialize':
D:/Users/alesg/Desktop/comp/SDL-mirror/src/core/windows/SDL_windows.c:95: undefined reference to `__imp_CoUninitialize'
./deps/windows/sdl/lib/libSDL2-static.a(SDL_windowskeyboard.c.obj): In function `IME_SetupAPI':
D:/Users/alesg/Desktop/comp/SDL-mirror/src/video/windows/SDL_windowskeyboard.c:650: undefined reference to `ImmGetIMEFileNameA'
D:/Users/alesg/Desktop/comp/SDL-mirror/src/video/windows/SDL_windowskeyboard.c:732: undefined reference to `ImmGetCompositionStringW'
D:/Users/alesg/Desktop/comp/SDL-mirror/src/video/windows/SDL_windowskeyboard.c:737: undefined reference to `ImmGetCompositionStringW'
./deps/windows/sdl/lib/libSDL2-static.a(SDL_windowskeyboard.c.obj): In function `IME_HandleMessage':
D:/Users/alesg/Desktop/comp/SDL-mirror/src/video/windows/SDL_windowskeyboard.c:901: undefined reference to `ImmReleaseContext'
./deps/windows/sdl/lib/libSDL2-static.a(SDL_windowskeyboard.c.obj): In function `IME_ClearComposition':
D:/Users/alesg/Desktop/comp/SDL-mirror/src/video/windows/SDL_windowskeyboard.c:716: undefined reference to `ImmGetContext'

Libnoise don't want to compile: Cannot find -lnoise

I'm trying to use the Libnoise library and I keep get this error:
F:\PortableApps\Documents\Adventure³>g++ Compiled/*.o -o Adventure3 -L./Libs -static -lglew32 -lglfw3dll -lopengl32 -lfreetype -lnoise -static-libgcc-static-libstdc++ Resources/icon.res
f:/portableapps/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lnoise
collect2.exe: error: ld returned 1 exit status
I have the libnoise.lib in my Libs folder. I tried with -llibnoise instead, and I get:
F:\PortableApps\Documents\Adventure³>g++ Compiled/*.o -o Adventure3 -L./Libs -static -lglew32 -lglfw3dll -lopengl32 -lfreetype -llibnoise -static-libgcc -static-libstdc++ Resources/icon.res
Compiled/Generator.o:Generator.cpp:(.text+0x3d): undefined reference to `noise::module::Perlin::GetValue(double, double, double) const'
Compiled/Generator.o:Generator.cpp:(.text+0x8a): undefined reference to `noise::module::Perlin::Perlin()'
Compiled/Generator.o:Generator.cpp:(.text$_ZN5noise6module6PerlinD1Ev[__ZN5noise6module6PerlinD1Ev]+0xa): undefined reference to `vtable for noise::module::Perlin'
Compiled/Generator.o:Generator.cpp:(.text$_ZN5noise6module6PerlinD1Ev[__ZN5noise6module6PerlinD1Ev]+0x19): undefined reference to `noise::module::Module::~Module()'
collect2.exe: error: ld returned 1 exit status
I don't know how to manually compile the source, it always return me a WinMain#16 error, no matter what I try to make.
Now, I really don't know what to do...
Anyone have a solution?
-l option of gcc supposes that library got name starting with lib, so -lnoise would link libnoise.
Usually .lib is a n import library or static library. The problems related to import libraries described here: How do import libraries work and why doesn't MinGW need them?
In relation to libnoise: Netbeans C++ using MinGW and the libnoise library

cUrl: Undefined reference

I am trying to use cUrl in my app but i am getting several errors. I already tried to find a solution on g or here, but nothing helped.
I build and link cUrl as static, have libcurl.a set up in linker, have proper includes but it still doesn't work.
I tried adding -lcurl into build settings but seems like it doesn't know that flag.
I am using cUrl 7.46.0 build with --disable--static and --without--ssl
and Code::Blocks ide & mingw compiler // c++ ofc
Here are those errors:
obj\Release\src\HTTPDownloadRequest.o:HTTPDownloadRequest.cpp| undefined reference to `_imp__curl_easy_cleanup'
obj\Release\src\HTTPDownloadRequest.o:HTTPDownloadRequest.cpp| undefined reference to `_imp__curl_easy_cleanup'
obj\Release\src\HTTPDownloadRequest.o:HTTPDownloadRequest.cpp| undefined reference to `_imp__curl_easy_init'
obj\Release\src\HTTPDownloadRequest.o:HTTPDownloadRequest.cpp| undefined reference to `_imp__curl_easy_setopt'
obj\Release\src\HTTPDownloadRequest.o:HTTPDownloadRequest.cpp| undefined reference to `_imp__curl_easy_perform'
Compilation command:
mingw32-g++.exe -LC:\WXWID\lib\gcc_lib -o bin\Release\app.exe obj\Release\app.o obj\Release\appMain.o obj\Release\appHistory.o obj\Release\src\HTTPDownloadRequest.o obj\Release\resource.res -s -static-libgcc -static-libstdc++ -static -mthreads -lwxmsw30u_core -lwxbase30u -lwxpng -lwxjpeg -lwxtiff -lwxzlib C:\WXWID\lib\gcc_lib\libwxmsw30u_adv.a "C:\curl\lib\libcurl.a" -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lcomctl32 -lwsock32 -lodbc32 -mwindows
Errors when used define CURL_STATICLIB
C:\curl\lib\libcurl.a(libcurl_la-connect.o)| undefined reference to `WSAIoctl#36'
C:\curl\lib\libcurl.a(libcurl_la-ldap.o)| undefined reference to `_imp__ldap_err2stringA'
C:\curl\lib\libcurl.a(libcurl_la-ldap.o)| undefined reference to `_imp__ldap_msgfree'
C:\curl\lib\libcurl.a(libcurl_la-ldap.o)|| undefined reference to `_imp__ldap_unbind_s'
...
If you try to link with static libcurl add -DCURL_STATICLIB to gcc arguments or define CURL_STATICLIB macro before including its headers to use correct mangling.
I would do
mingw32-g++.exe -LC:\curl\lib -lcurl ................

libcurl static link - undefined reference to `_imp__WSAIoctl#36'

I'm trying to statically link libcurl in codeblocks on Windows.
I compiled libcurl with "mingw32-make mingw32", and have linked the resulting libraries libcurl.a and libcurldll.a. I saw another post on ere that suggested the solution was to add option -lwldap32, but that's made no difference :/
What am I missing?
EDIT: Here's a sample of the first compile command that completed successfully:
mingw32-g++.exe -Wall -pipe -mthreads -std=gnu++14 -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -O2 -DCURL_STATICLIB -IE:\plus\libs\wxWidgets\include -IE:\plus\libs\curl-7.43.0\include -IE:\plus\libs\wxWidgets\lib\gcc_lib\mswu -c E:\plus\projects\hlr\hlrMain.cpp -o obj\Release\hlrMain.o
E:\plus\projects\hlr\hlrMain.cpp:165:1: warning: 'virtual bool wxEvtHandler::TryValidator(wxEvent&)' is deprecated [-Wdeprecated-declarations]
}
^
In file included from E:\plus\libs\wxWidgets\include/wx/menu.h:14:0,
from E:\plus\projects\hlr\hlrMain.h:14,
from E:\plus\projects\hlr\hlrMain.cpp:10:
E:\plus\libs\wxWidgets\include/wx/event.h:3683:22: note: declared here
virtual bool TryValidator(wxEvent& WXUNUSED(event)), return false; )
^
E:\plus\libs\wxWidgets\include/wx/defs.h:615:43: note: in definition of macro 'wxDEPRECATED'
#define wxDEPRECATED(x) wxDEPRECATED_DECL x
^
E:\plus\libs\wxWidgets\include/wx/event.h:3682:5: note: in expansion of macro 'wxDEPRECATED_BUT_USED_INTERNALLY_INLINE'
wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
^
E:\plus\projects\hlr\hlrMain.cpp:165:1: warning: 'virtual bool wxEvtHandler::TryParent(wxEvent&)' is deprecated [-Wdeprecated-declarations]
}
Link to full output with lots of warnings here: http://pastebin.com/a6s0mwQ7
Failed compile:
mingw32-g++.exe -LE:\plus\libs\wxWidgets\lib\gcc_lib -o bin\Release\hlr.exe obj\Release\hlrApp.o obj\Release\hlrMain.o obj\Release\resource.res -s -lwldap32 -mthreads -lwxmsw30u -lwxpng -lwxjpeg -lwxtiff -lwxzlib E:\plus\libs\curl-7.43.0\lib\libcurl.a E:\plus\libs\curl-7.43.0\lib\libcurldll.a -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lcomctl32 -lwsock32 -lodbc32 -mwindows
E:\plus\libs\curl-7.43.0\lib\libcurl.a(connect.o):(.text+0xbda): undefined reference to `_imp__WSAIoctl#36'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x145): undefined reference to `_imp__ldap_err2string'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x187): undefined reference to `_imp__ldap_msgfree'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x1b4): undefined reference to `_imp__ldap_unbind_s'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x41f): undefined reference to `_imp__ldap_set_option'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x444): undefined reference to `_imp__ldap_init'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x47b): undefined reference to `_imp__ldap_simple_bind_s'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x4f1): undefined reference to `_imp__ldap_err2string'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x5a9): undefined reference to `_imp__ldap_search_s'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x5c2): undefined reference to `_imp__ldap_err2string'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x5f1): undefined reference to `_imp__ldap_first_entry'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x62c): undefined reference to `_imp__ldap_get_dn'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x6cc): undefined reference to `_imp__ldap_memfree'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x6e9): undefined reference to `_imp__ldap_first_attribute'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x71e): undefined reference to `_imp__ldap_get_values_len'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x911): undefined reference to `_imp__ldap_value_free_len'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x91e): undefined reference to `_imp__ldap_memfree'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x93b): undefined reference to `_imp__ber_free'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x983): undefined reference to `_imp__ldap_value_free_len'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x990): undefined reference to `_imp__ldap_memfree'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0x9ef): undefined reference to `_imp__ldap_next_attribute'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0xa14): undefined reference to `_imp__ber_free'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0xa29): undefined reference to `_imp__ldap_next_entry'
E:\plus\libs\curl-7.43.0\lib\libcurl.a(ldap.o):(.text+0xa69): undefined reference to `_imp__ldap_memfree'
For anyone else having this problem, turned out that I needed to add the ws2_32 lib by adding a linker settings->link library in code::blocks, using the full filename libws2_32.a.
Adding -lws2_32 as suggested elsewhere online wasn't enough for some reason.
Compile command now is:
mingw32-g++.exe -LE:\plus\libs\wxWidgets\lib\gcc_lib -o bin\Release\hlr.exe obj\Release\hlrApp.o obj\Release\hlrMain.o obj\Release\resource.res -s -mthreads -lwxmsw30u -lwxpng -lwxjpeg -lwxtiff -lwxzlib -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lcomctl32 -lwsock32 -lodbc32 E:\plus\libs\curl-7.43.0\lib\libcurl.a E:\plus\compilers\MinGW\i686-w64-mingw32\lib\libws2_32.a E:\plus\compilers\MinGW\i686-w64-mingw32\lib\libwldap32.a -mwindows
Curl depends on OpenSSL. Make sure you have linked it.
_imp__WSAIoctl is a part of "Ws2.lib"(Ws2_32.lib), header "winsock2.h". Try make sure that this reference is resolved in your project.

Linking libcurl while cross compiling with mingw32 under Linux for Windows

I have compiled libcurl using mingw32 and am trying to link it with my program using mingw32 for a Windows system from my Linux machine.
I was outputted the files, libcurl-4.dll libcurl.a libcurl.la libcurl.lai.
I have included them in my mingw32 libs folder at: /usr/x86_64-w64-mingw32/lib
I was able to find a few other topics on linking with the libstdc++ and libgcc to take care dependency errors while executed but when trying to add libcurl.a it will not compile period.
I used the following:
$ x86_64-w64-mingw32-g++ main.cpp -o hello.exe -static-libgcc -static-libstdc++ -static "/usr/x86_64-w64-mingw32/lib/libcurl.a" -lpthread
However, I cannot not get it to use the libcurl.a and am continuing to receive these errors.
/tmp/ccIceRus.o:main.cpp:(.text+0xde): undefined reference to `__imp_curl_easy_init'
/tmp/ccIceRus.o:main.cpp:(.text+0x106): undefined reference to `__imp_curl_easy_setopt'
/tmp/ccIceRus.o:main.cpp:(.text+0x122): undefined reference to `__imp_curl_easy_setopt'
/tmp/ccIceRus.o:main.cpp:(.text+0x13e): undefined reference to `__imp_curl_easy_setopt'
/tmp/ccIceRus.o:main.cpp:(.text+0x159): undefined reference to `__imp_curl_easy_setopt'
/tmp/ccIceRus.o:main.cpp:(.text+0x169): undefined reference to `__imp_curl_easy_perform'
/tmp/ccIceRus.o:main.cpp:(.text+0x180): undefined reference to `__imp_curl_easy_strerror'
/tmp/ccIceRus.o:main.cpp:(.text+0x197): undefined reference to `__imp_curl_easy_cleanup'
/usr/bin/x86_64-w64-mingw32-ld: /tmp/ccIceRus.o: bad reloc address 0x80 in section `.xdata'
collect2: error: ld returned 1 exit status
What am I doing wrong?. I can not get past this. I know it has to be some stupid issue.
Thank you.
I was able to solve the question by specifying -DCURL_STATICLIB, as well as linking some other dependencies.
x86_64-w64-mingw32-g++ main.cpp -o hello.exe -DCURL_STATICLIB -static -lstdc++ -lgcc -lpthread -lcurl -lwldap32 -lws2_32