如何在 Linux 中使用 CMake 和 Kdevelop 编译 GLUT + OpenGL 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9460242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:50:50  来源:igfitidea点击:

How to compile GLUT + OpenGL project with CMake and Kdevelop in linux?

linuxopenglcmake

提问by Ren

As the titles says I can't seem to build the project with OpenGL and Glut.

正如标题所说,我似乎无法使用 OpenGL 和 Glut 构建项目。

I get Undefined reference errors for OpenGL functions.

我收到 OpenGL 函数的未定义引用错误。

I tried doing :

我试着做:

project(testas)
find_package(OpenGL)
find_package(GLUT)
add_executable(testas main.cpp)

But that doesn't work.

但这不起作用。

Any suggestions?

有什么建议?

采纳答案by simont

find_package(OpenGL)will find the package for you, but it doesn't link the package to the target.

find_package(OpenGL)会为您找到包,但它不会将包链接到目标。

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.

要链接到库,您可以使用target_link_libraries(<target> <item>). 此外,您还需要设置include directory,以便链接器知道去哪里寻找东西。这是通过include_directories.

An example CMakeLists.txtwhich would do this looks something like this:

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} )

If OpenGLis a necessity for your project, you might consider either testing OpenGL_FOUNDafter the find_package(OpenGL)or using REQUIRED, which will stop cmakeif OpenGLis not found.

如果OpenGL您的项目需要,您可以考虑OpenGL_FOUNDfind_package(OpenGL)或之后进行测试REQUIREDcmake如果OpenGL未找到,它将停止。

For more information and better examples:

有关更多信息和更好的示例:

In particular, the CMake wikiand cmake and opengllinks should give you enough to get things working.

特别是,CMake wikicmake and opengl链接应该足以让您工作。

回答by wangzheqie

I use these two cmake files to build my OpenGL projects, and they all work well.

我使用这两个 cmake 文件来构建我的 OpenGL 项目,它们都运行良好。

I only test these two cmake files under Deepin Linux. Deepin Linux is a Chinese grown Linux system like Ubuntu or from Debian.

我只在Deepin Linux下测试了这两个cmake文件。Deepin Linux 是一个中国成长的 Linux 系统,如 Ubuntu 或来自 Debian。

First, the main CMakeLists.txt

一、主CMakeLists.txt

cmake_minimum_required(VERSION 3.1.0)
project(project_name)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")

find_package(OpenGL REQUIRED)
find_package(FREEGLUT REQUIRED)
find_package(GLEW REQUIRED)

if(NOT ${OPENGL_FOUND})
    message("OPENGL not found")
endif()

include_directories(
    ${PROJECT_SOURCE_DIR}
    ${FREEGLUT_INCLUDE_DIR}
    ${GLEW_INCLUDE_DIR}
    ${OPENGL_INCLUDE_DIR}
    )

message(${OPENGL_INCLUDE_DIR})
add_executable(${PROJECT_NAME}  ${PROJECT_SOURCE_DIR}/filename.cpp) 
target_link_libraries(${PROJECT_NAME} 
${OPENGL_LIBRARY}
${FREEGLUT_LIBRARY}
${GLEW_LIBRARY}
)

Second, the find GLUT cmake file under CMakeModules directory

二、在CMakeModules目录下找到GLUT cmake文件

# Try to find the FREEGLUT library
#
# FREEGLUT_INCLUDE_DIR
# FREEGLUT_LIBRARY
# FREEGLUT_FOUND

FIND_PATH(
  FREEGLUT_INCLUDE_DIR GL/freeglut.h GL/gl.h GL/glu.h GL/glew.h
  ${CMAKE_INCLUDE_PATH}
  $ENV{include}
  ${OPENGL_INCLUDE_DIR}
  /usr/include
  /usr/local/include
)

SET(STORE_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
SET(CMAKE_FIND_FRAMEWORK NEVER)

FIND_LIBRARY(
  FREEGLUT_LIBRARY
  NAMES freeglut_static freeglut glut GL
  PATH
    /opt/local/lib
    ${CMAKE_LIBRARY_PATH}
    $ENV{lib}
    /usr/lib
    /usr/local/lib
)

SET(CMAKE_FIND_FRAMEWORK ${STORE_CMAKE_FIND_FRAMEWORK})

IF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)
   SET(FREEGLUT_FOUND TRUE)
ENDIF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)

IF (FREEGLUT_FOUND)
   IF (NOT FREEGLUT_FIND_QUIETLY)
      MESSAGE(STATUS "Found FREEGLUT: ${FREEGLUT_LIBRARY}")
   ENDIF (NOT FREEGLUT_FIND_QUIETLY)
ELSE (FREEGLUT_FOUND)
   IF (FREEGLUT_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find FREEGLUT")
   ENDIF (FREEGLUT_FIND_REQUIRED)
ENDIF (FREEGLUT_FOUND)

回答by minexew

In recent versions of CMake (3.10+) there is a new way to use OpenGL using a so-called IMPORTED target:

在 CMake (3.10+) 的最新版本中,有一种使用所谓的IMPORTED 目标来使用 OpenGL 的新方法:

cmake_minimum_required(VERSION 3.10)

project(testas)
add_executable(testas main.cpp)
find_package(OpenGL REQUIRED COMPONENTS OpenGL)
find_package(GLUT REQUIRED)

add_dependencies(testas OpenGL::OpenGL)
include_directories(${GLUT_INCLUDE_DIRS} )

target_link_libraries(testas OpenGL::OpenGL ${GLUT_LIBRARY} )

At the moment the only practical difference seems to be on Linux (where GLVND is used if available), but presumably this solution should be more future-proof, as CMake has more information about your intentions and other tools parsing your CMakeFiles will better understand the dependency tree.

目前唯一的实际区别似乎是在 Linux 上(如果可用,则使用 GLVND),但据推测,此解决方案应该更具前瞻性,因为 CMake 有更多关于您的意图的信息,而其他解析您的 CMakeFiles 的工具将更好地理解依赖树。