2016-09-15 70 views
5

所以,我使用freeglut尝试做一些openGL的东西,但我不断收到错误说引用未定义在OpenGL链接错误:使用freeglut在克利翁

CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper4initEv': 
.../TextureMapper.cpp:20: undefined reference to `[email protected]' 
.../TextureMapper.cpp:23: undefined reference to `[email protected]' 
.../TextureMapper.cpp:24: undefined reference to `[email protected]' 
.../TextureMapper.cpp:25: undefined reference to `[email protected]' 
CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper7displayEv': 
.../TextureMapper.cpp:45: undefined reference to `[email protected]' 
...TextureMapper.cpp:48: undefined reference to `[email protected]' 
...TextureMapper.cpp:49: undefined reference to `[email protected]' 
...TextureMapper.cpp:52: undefined reference to `[email protected]' 
...TextureMapper.cpp:53: undefined reference to `[email protected]' 
...TextureMapper.cpp:54: undefined reference to `[email protected]' 
...TextureMapper.cpp:55: undefined reference to `[email protected]' 
...TextureMapper.cpp:58: undefined reference to `[email protected]' 
...TextureMapper.cpp:61: undefined reference to `[email protected]' 

我使用的MinGW与克利翁以做这个项目。我以为我把一切都正确了。我将相应的文件移动到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

您的问题有与CLion无关。这只是关于CMake和你的环境。 – Sergey

回答

1

你实际上并没有将OpenGL链接到你的项目,所以你得到了未定义的OpenGL函数引用。试着用

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a GL) 

我与你CMakeLists.txt转载您的问题,下面的程序更换

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a) 

#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 

UPDATE

this,你有一些变量来访问您的实际OpenGL库。例如,你可能指向直接OpenGL库文件(S)是这样的:

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a ${OPENGL_gl_LIBRARY}) 

你也可以添加OpenGL库目录到library search pathtarget_link_libraries之前做到这一点):

link_directories(${OPENGL_gl_LIBRARY}) 
+0

我得到'c:/ mingw/bin /../ lib/gcc/mingw32/4.8.1 /../../../../ mingw32/bin/ld.exe:找不到-lGL'这是一个结果。 –

+0

@CacheStaheli我更新了我的答案。这应该会考虑到您的实际环境。 – Sergey

+0

只要在'target_link_libraries'中添加'link_directories'调用以及额外的库('$ {OPENGL_gl_LIBRARY}'),它就可以很好地工作。谢谢! –