如何在linux中使用CMake和Kdevelop编译GLUT + OpenGL项目? [英] How to compile GLUT + OpenGL project with CMake and Kdevelop in linux?
问题描述
由于标题说我似乎不能用OpenGL和Glut构建项目。
As the titles says I can't seem to build the project with OpenGL and Glut.
我得到OpenGL函数的未定义的引用错误。
I get Undefined reference errors for OpenGL functions.
我试过:
project(testas)
find_package(OpenGL)
find_package(GLUT)
add_executable(testas main.cpp)
任何建议?
推荐答案
find_package(OpenGL)
将为您找到该软件包,但它不会将软件包链接到目标。
find_package(OpenGL)
will find the package for you, but it doesn't link the package to the target.
要链接到库,您可以使用 target_link_libraries(< target>< item>)
。此外,您还需要设置 include目录
,以便链接器知道在哪里查找内容。这是通过 include_directories
完成的。
To link to a library, you can use target_link_libraries(<target> <item>)
. In addition, you also need to set the include directory
, so that the linker knows where to look for things. This is done with the include_directories
.
一个示例 CMakeLists.txt
可以这样做:
cmake_minimum_required(VERSION 2.8)
project(testas)
add_executable(testas main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )
target_link_libraries(testas ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
如果 OpenGL
项目,你可以考虑在 find_package(OpenGL)
之后测试 OpenGL_FOUND
,或者使用 REQUIRED
,如果未找到 OpenGL
,它将停止 cmake
If OpenGL
is a necessity for your project, you might consider either testing OpenGL_FOUND
after the find_package(OpenGL)
or using REQUIRED
, which will stop cmake
if OpenGL
is not found.
有关详细信息和更好的示例:
For more information and better examples:
- CMake 2.8文档, target_link_libraries
- CMake 2.8文档, find_package
- CMake wiki:如何查找图书馆
- 论坛帖子解决方案: cmake和opengl
- swarthmore.edu
的教学课程
- CMake 2.8 documentation, target_link_libraries
- CMake 2.8 documentation, find_package
- CMake wiki: how to find libraries
- Forum post with solution: cmake and opengl
- Tutorial for CMake by swarthmore.edu
特别地, CMake wiki
和 cmake和opengl
链接给你足够的东西工作。
In particular, the CMake wiki
and cmake and opengl
links should give you enough to get things working.
这篇关于如何在linux中使用CMake和Kdevelop编译GLUT + OpenGL项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!