将 GLEW 的静态二进制文件与 CMake 链接时未定义对 GLEW 函数的引用

发布于 2025-01-09 02:30:06 字数 2636 浏览 0 评论 0原文

问题

glew32s.lib 未与主可执行文件正确链接。当使用 CMake 给出的 MakeFile 进行构建时,会发生错误,指出我对 glewInit() 有未定义的引用。

CMakeFiles\NoGameEngine.dir/objects.a(window.cpp.obj):window.cpp:(.text+0x15d):对“glewInit”的未定义引用

但是在我的顶级 CMakeLists.txt我已通过以下方式链接了glew32s.lib:

target_link_libraries( ${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/Dependencies/glew32s.lib)

我完全不知道为什么会收到此未定义的引用错误。注意我还声明了编译选项-DGLEW_STATIC

我尝试过

使用另一种方式链接库:

target_link_directories( ${PROJECT_NAME} PRIVATE Dependencies)
target_link_libraries( ${PROJECT_NAME} PRIVATE glew32s.lib)

但是我收到以下错误:

C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld .exe:跳过不兼容C:/PROGRA~4/C__~1/Projects/GAMEPR~1/NOGAME~1/DEPEND~1/glew32s.lib 时 搜索 -lglew32s C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe:跳过不兼容C:/PROGRA~4/C__~1/Projects/GAMEPR~1/NOGAME~1/DEPEND~1/glew32s.lib 时 搜索 -lglew32s C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe:跳过不兼容C:/PROGRA~4/C__~1/Projects/GAMEPR~1/NOGAME~1/DEPEND~1\glew32s.lib 当 搜索 -lglew32s C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe:找不到-lglew32s

资源

CMakeLists.txt:

cmake_minimum_required(VERSION 3.23.)

set(CMAKE_CXX_STANDARD 17)
set(SourceFiles-Core
    NoGameEngine-core/src/main.cpp
    NoGameEngine-core/src/graphics/window.cpp
)
set(includeFiles-Core NoGameEngine-core/include)
set (includeFiles-Depend Dependencies/include)
add_compile_options(-DGLEW_STATIC)

find_package( OpenGL REQUIRED )

if (OPENGL_FOUND)
  MESSAGE("OpenGL Correctly Found")
else (OPENGL_FOUND)
  MESSAGE("OpenGL environment missing")
endif()

project(NoGameEngine)


include_directories( ${OPENGL_INCLUDE_DIRS} ${includeFiles-Core} ${includeFiles-Depend})

set( GLFW_BUILD_DOCS OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_INSTALL OFF CACHE BOOL  "GLFW lib only" )

add_subdirectory( Dependencies/GLFW )

add_executable(${PROJECT_NAME} ${SourceFiles-Core})
target_link_libraries( ${PROJECT_NAME} PRIVATE ${OPENGL_LIBRARIES} glfw)
target_link_libraries( ${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/Dependencies/glew32s.lib)

目录层次结构:

根:

  • 构建
  • 依赖项
    • GLFW
    • 包括
      • GL
    • glew32s.lib
  • NoGameEngine-核心
    • 包括
    • 来源
  • CMakeLists.txt

Problem

glew32s.lib is not linking correctly with the main executable. When building with MakeFile given by CMake, an error occurs stating that I have an undefined reference to glewInit().

CMakeFiles\NoGameEngine.dir/objects.a(window.cpp.obj):window.cpp:(.text+0x15d): undefined reference to `glewInit'

However in my top-level CMakeLists.txt I have linked the glew32s.lib through:

target_link_libraries( ${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/Dependencies/glew32s.lib)

I am completely clueless to why I am getting this undefined reference error. Note I have also declared the compile option -DGLEW_STATIC.

What I have tried

Using another way to link the library:

target_link_directories( ${PROJECT_NAME} PRIVATE Dependencies)
target_link_libraries( ${PROJECT_NAME} PRIVATE glew32s.lib)

However I get the following errors:

C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/PROGRA~4/C__~1/Projects/GAMEPR~1/NOGAME~1/DEPEND~1/glew32s.lib when
searching for -lglew32s
C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/PROGRA~4/C__~1/Projects/GAMEPR~1/NOGAME~1/DEPEND~1/glew32s.lib when
searching for -lglew32s
C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/PROGRA~4/C__~1/Projects/GAMEPR~1/NOGAME~1/DEPEND~1\glew32s.lib when
searching for -lglew32s
C:/MinGW64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lglew32s

Resources

CMakeLists.txt:

cmake_minimum_required(VERSION 3.23.)

set(CMAKE_CXX_STANDARD 17)
set(SourceFiles-Core
    NoGameEngine-core/src/main.cpp
    NoGameEngine-core/src/graphics/window.cpp
)
set(includeFiles-Core NoGameEngine-core/include)
set (includeFiles-Depend Dependencies/include)
add_compile_options(-DGLEW_STATIC)

find_package( OpenGL REQUIRED )

if (OPENGL_FOUND)
  MESSAGE("OpenGL Correctly Found")
else (OPENGL_FOUND)
  MESSAGE("OpenGL environment missing")
endif()

project(NoGameEngine)


include_directories( ${OPENGL_INCLUDE_DIRS} ${includeFiles-Core} ${includeFiles-Depend})

set( GLFW_BUILD_DOCS OFF CACHE BOOL  "GLFW lib only" )
set( GLFW_INSTALL OFF CACHE BOOL  "GLFW lib only" )

add_subdirectory( Dependencies/GLFW )

add_executable(${PROJECT_NAME} ${SourceFiles-Core})
target_link_libraries( ${PROJECT_NAME} PRIVATE ${OPENGL_LIBRARIES} glfw)
target_link_libraries( ${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/Dependencies/glew32s.lib)

Directory Hierarchy:

Root:

  • build
  • Dependencies
    • GLFW
    • include
      • GL
    • glew32s.lib
  • NoGameEngine-core
    • include
    • src
  • CMakeLists.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文