Ilya  Sokolov
Ilya Sokolov

Reputation: 17

undefined reference to Xlib and Opengl symbols under WSL

I have some issues with linking X11 and Opengl libraries while cross-compiling application for Linux under WSL using Visual Studio 2022.

It seems that all settings are correct, the X11 and Opengl libraries have been included in the command line, so the line looks like:

x86_64-linux-gnu-g++ -o "/home/sinsin/projects/LibForLinux/bin/x64/Debug/ServerStubProjectLinux.so" -Wl,--no-undefined -Wl,-L"/usr/lib/x86_64-linux-gnu/" -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -shared -pthread -L/usr/lib/x86_64-linux-gnu  -lm -lrt -lX11 -lglut -lGL -lGLEW -lGLU /home/sinsin/projects/LibForLinux/obj/x64/Debug/cppLibraryRealClass.o /home/sinsin/projects/LibForLinux/obj/x64/Debug/OpenglWnd.o /home/sinsin/projects/LibForLinux/obj/x64/Debug/pch.o /home/sinsin/projects/LibForLinux/obj/x64/Debug/ReadDummyArrayClass.o /home/sinsin/projects/LibForLinux/obj/x64/Debug/RealStringClass.o -static-libstdc++

Both libX11.so and libGL.so are present in the /usr/lib/x86_64-linux-gnu/ directory, however, the wrong linkage error, shown below, persists:

1>  D:\Projects\ForLinuxTranslation\Current\ServerStubProject\ServerStubProject\OpenglWnd.cpp(135): error : undefined reference to `XOpenDisplay'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:141: undefined reference to `glXQueryExtension'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:147: undefined reference to `glXChooseVisual'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:154: undefined reference to `glXCreateContext'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:161: undefined reference to `XCreateColormap'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:166: undefined reference to `XCreateWindow'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:169: undefined reference to `XSetStandardProperties'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:172: undefined reference to `glXMakeCurrent'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:174: undefined reference to `XMapWindow'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:175: undefined reference to `XIfEvent'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/obj/x64/Debug/OpenglWnd.o: in function `OpenglWnd::resize(int, int)':
1>  D:\Projects\ForLinuxTranslation\Current\ServerStubProject\ServerStubProject\OpenglWnd.cpp(245): error : undefined reference to `glViewport'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:249: undefined reference to `glMatrixMode'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:250: undefined reference to `glLoadIdentity'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:251: undefined reference to `gluPerspective'
1>  /usr/bin/ld : error : /home/sinsin/projects/LibForLinux/OpenglWnd.cpp:252: undefined reference to `glMatrixMode'

It seems that somehow the linker have found the library, because file not found error would be in other case, however linker does not link neither the X11 nor Opengl library.

Could you give me any clue where to look?

Upvotes: -2

Views: 38

Answers (1)

AppsderAI
AppsderAI

Reputation: 1

Summary of Fixes:

-- Change library order (move -lX11 and -lGL to the end). -- Explicitly add -lGLX for missing glX* functions. -- Install libx11-dev, libgl1-mesa-dev, libglu1-mesa-dev, libglew-dev, freeglut3-dev. -- Remove -static-libstdc++ if needed. -- Ensure DISPLAY is set properly in WSL.

Upvotes: -2

Related Questions