c++ - 在 CLion 中使用 freeglut 在 OpenGL 中链接错误

标签 c++ opengl cmake mingw freeglut

所以,我正在使用 freeglut 尝试做一些 openGL 的事情,但我不断收到错误消息,说引用未定义:

CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper4initEv':
.../TextureMapper.cpp:20: undefined reference to `glClearColor@16'
.../TextureMapper.cpp:23: undefined reference to `glMatrixMode@4'
.../TextureMapper.cpp:24: undefined reference to `glLoadIdentity@0'
.../TextureMapper.cpp:25: undefined reference to `glOrtho@48'
CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper7displayEv':
.../TextureMapper.cpp:45: undefined reference to `glClear@4'
...TextureMapper.cpp:48: undefined reference to `glColor3f@12'
...TextureMapper.cpp:49: undefined reference to `glBegin@4'
...TextureMapper.cpp:52: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:53: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:54: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:55: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:58: undefined reference to `glEnd@0'
...TextureMapper.cpp:61: undefined reference to `glFlush@0'

我正在使用 MinGW 和 CLion 来完成这个项目。我以为我做对了一切。我将适当的文件移动到 MinGW 中的 include 文件夹,以及 bin 文件夹和 lib 文件夹。然后,我的 CMakeLists.txt 中有这个:

cmake_minimum_required(VERSION 3.3)
project(texture_mapping)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp TextureMapper.cpp TextureMapper.h Vertex.h ObjParser.cpp ObjParser.h)

add_executable(texture_mapping ${SOURCE_FILES})
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a)

我链接的库是 freeglut 附带的唯一库文件。

那么,我错过了什么? CLion 在编译之前不会显示任何错误。我什至可以进入 freeglut 提供的头文件中的函数。那为什么我的程序中没有定义这些函数呢?

最佳答案

我一直在努力解决同样的问题,并通过将以下行附加到 CMakeLists.txt 来解决它:

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

关于c++ - 在 CLion 中使用 freeglut 在 OpenGL 中链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39502392/

相关文章:

html - 在 windows 下,WebGL 使用 DirectX 运行时或 OpenGL 运行时?

c++ - 在 OpenGL 中围绕对象而不是相机周围的对象旋转 'camera'

opengl - 什么是 OpenGL 中的阴影采样器以及它们的可能用途?

c++ - 将打包结构成员的引用传递给模板。海湾合作委员会错误?

c++ - 数据结构对齐:char数组和std::string数组为什么不同

c++ - 套接字和线程

xcode - opengl 教程 xcode cmake

c++ - 将 C 和 C++ 与 CMAKE 混合

Android NDK CMake 链接问题

c++ - 使用容器中元素的别名使用 std::list::remove 删除元素是否正确?