2016-09-15 44 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}'),它就可以很好地工作。謝謝! –