所以,我使用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提供的头文件中的函数。那么,为什么我的程序中没有定义这些函数?
答案 0 :(得分:1)
您实际上没有将OpenGL链接到您的项目,因此您获得了对OpenGL函数的未定义引用。尝试替换
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a)
与
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a GL)
我使用您的CMakeLists.txt
和以下程序重现了您的问题:
#include <GL/gl.h>
int main() {
glClear(GL_COLOR_BUFFER_BIT);
return 0;
}
并通过上述替换解决了这个问题。该解决方案自动链接GL库与我的库路径:
$ ls -1 /usr/lib64/libGL.*
/usr/lib64/libGL.la
/usr/lib64/libGL.so
/usr/lib64/libGL.so.1
/usr/lib64/libGL.so.1.0.0
<强>更新强>
根据this,您有一些变量可以访问您的实际OpenGL库。例如,您可以直接指向OpenGL库文件:
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a ${OPENGL_gl_LIBRARY})
您也可以将OpenGL库目录添加到library search path(在target_link_libraries
之前执行):
link_directories(${OPENGL_gl_LIBRARY})
答案 1 :(得分:1)
我一直在努力解决相同的问题,并通过在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})
答案 2 :(得分:0)
重新排列CMakeLists.txt
对我有帮助(<name>
应该相应地替换):
cmake_minimum_required(VERSION 3.10)
project(<name>)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lGL -lGLU -lglut")
set(CMAKE_CXX_STANDARD 17)
add_executable(<name> main.cpp)
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})