Search code examples
c++cmakelinker-errorsglew

Linking statically GLEW with CMAKE


I know this subject is quite well documented and I tried looking for an awnser but I couldn't find any for my case.

I'm trying to link statically the windows libraries of GLEW and I get different mistakes depending what I do.

First here is my CMAKE file :

cmake_minimum_required(VERSION 3.24)
project(Physics_Engine)

set(CMAKE_CXX_STANDARD 17)


add_executable(${PROJECT_NAME} src/main.cpp)

#target_compile_definitions(${PROJECT_NAME} PRIVATE GLEW_STATIC)


find_package(OpenGL REQUIRED)


add_subdirectory(lib/GLFW)

include_directories(lib/GLEW-WIN-LIB/include)
target_link_directories(${PROJECT_NAME} PUBLIC lib/GLEW-WIN-LIB/lib/Release/x64)

target_link_libraries(${PROJECT_NAME} opengl32 glfw glew32s)

And then my two kinds of problems :

1/

If I keep the make file as is, I get this error :

[1/1] Linking CXX executable Physics_Engine.exe
FAILED: Physics_Engine.exe 
cmd.exe /C "cd . && D:\JetBrains\CLion\bin\mingw\bin\g++.exe -g  CMakeFiles/Physics_Engine.dir/src/main.cpp.obj -o Physics_Engine.exe -Wl,--out-implib,libPhysics_Engine.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -LC:/Users/matts/Documents/Projects/physics-engine-cpp/lib/GLEW-WIN-LIB/lib/Release/x64 -lopengl32  lib/GLFW/src/libglfw3.a  -lglew32s  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."


D:\JetBrains\CLion\bin\mingw\bin/ld.exe: CMakeFiles/Physics_Engine.dir/src/main.cpp.obj:C:/Users/matts/Documents/Projects/physics-engine-cpp/src/main.cpp:12: undefined reference to `__imp_glewInit'

2/

If I add the compile definitions :

[2/2] Linking CXX executable Physics_Engine.exe
FAILED: Physics_Engine.exe 
cmd.exe /C "cd . && D:\JetBrains\CLion\bin\mingw\bin\g++.exe -g  CMakeFiles/Physics_Engine.dir/src/main.cpp.obj -o Physics_Engine.exe -Wl,--out-implib,libPhysics_Engine.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -LC:/Users/matts/Documents/Projects/physics-engine-cpp/lib/GLEW-WIN-LIB/lib/Release/x64 -lopengl32  lib/GLFW/src/libglfw3.a  -lglew32s  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."


Warning: corrupt .drectve at end of def file
D:\JetBrains\CLion\bin\mingw\bin/ld.exe: C:/Users/matts/Documents/Projects/physics-engine-cpp/lib/GLEW-WIN-LIB/lib/Release/x64/glew32s.lib(tmp/glew_static/Release/x64/glew.obj):(.text$mn+0xd): undefined reference to `__imp_wglGetProcAddress'

With 10 other lines like the last one

I tried using the target_compile_definitions; the compile_definitions and the add_definitions functions one at a time without it working...

What am I doing wrong?


Solution

  • The second error can be solved by moving glew32s to the front of the linker inputs. The linker only resolves unresolved symbols from libraries that came earlier in the linker list. Change your final line to:

    target_link_libraries(${PROJECT_NAME} glew32s opengl32 glfw)
    

    Alternatively, you could use the nice built-in CMake build support for GLEW:

    set(GLEW_USE_STATIC_LIBS ON)
    find_package(GLEW REQUIRED)
    target_link_libraries(${PROJECT_NAME} glfw GLEW::GLEW)
    

    Note that you may have to give CMake some hints in the form of CMAKE_PREFIX_PATH. See the documentation of the built-in find_path function.