【问题标题】:Running Opengl program with CLion IDE [duplicate]使用 CLion IDE 运行 Opengl 程序
【发布时间】:2018-04-16 14:42:54
【问题描述】:

我知道已经有人问过这个问题,但提供的解决方案确实对我不起作用。

我想通过 CLion IDE 运行我的 OpenGL 程序。我可以使用 Ubuntu 通过终端运行相同的程序

$gcc progname.c -lglut -lGL -lGLU

但我无法在 CLion 中运行相同的程序

这是我的 CMakeLists.txt 文件

PS:我在 Ubuntu 中使用 CLion。程序也没有错误。

【问题讨论】:

  • 我也做了set(CMAKE_CXX_FLAGS "-lglut -lGL -lGLU")
  • 在尝试运行时,CLion 是否会打印任何错误消息或其他输出?

标签: c++ cmake


【解决方案1】:

Clion 将 CMake 用于项目文件,并且 CMake 使用 target_link_libraries 可以轻松链接系统路径中的库,您可以在如下所示的情况下使用它,它可以在 之后 add_executable 行:

target_link_libraries(jarvis -lglut -lGL -lGLU)

target_link_libraries 的文档在这里: https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html

【讨论】:

  • 谢谢!成功了
【解决方案2】:

在 Google 上进行了大量搜索后,我已使其在 Windows 10 和 Linux (Ubuntu 16.04) 中运行。显然,它毕竟不是那么容易找到。所以,我现在和这里要结束这个问题。

在这里,我将向您展示如何配置 CMakeLists.txt 文件来编译 OpenGL 程序,这是这里的主要挑战。我假设您可以编写基本的 OpenGL 程序并且您已经编写了一个名为 'demoMain.cpp'。

对于 Windows

我假设您可以在 Windows 上设置 OpenGL。如果你不能,youtube 和 StackOverflow 上有很多教程。之后,继续。

cmake_minimum_required(VERSION 3.10)
project(Graphics_Offline_1) # Your Project Name

set(CMAKE_CXX_STANDARD 11)

include_directories(OpenGL)
include_directories(OpenGL/include) # OpenGL/include has to contain the required OpenGL's .h files
include_directories(OpenGL/lib) # OpenGL/lib has to contain the required OpenGL's .lib files


# glut32.dll must be present in "project-directory/OpenGL/dll/"


add_custom_target(glutdlllib
    COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/OpenGL/dll/glut32.dll ${CMAKE_BINARY_DIR}
    )

# required .lib files to be copied into compiler's proper directory
set(OpenGlLibs glaux glu32 glui32 glut32 opengl32)


#These 3 lines are just linking and making executables

add_executable(demo demoMain.cpp)

target_link_libraries(demo ${OpenGlLibs})

add_dependencies(demo glutdlllib)

适用于 Linux (Ubuntu 16.04)

它也应该适用于其他 Ubuntu 版本。 Linux 使 OpenGL 的使用比 Windows 更容易。

cmake_minimum_required(VERSION 3.10) # common to every CLion project
project(OpenGLLinuxTest) # project name


set(OpenGlLinkers -lglut -lGLU -lGL) # setting all the Glut libraries as one variable.


################################################

add_executable(OpenGLLinuxTest1 main.cpp ) #common to all clion project
target_link_libraries(OpenGLLinuxTest1 ${OpenGlLinkers}) # linking opengl libraries to the project

#################################################

我假设您可以在 Ubuntu 上安装 OpenGL。如果你遇到这个问题,

点击此链接 - http://www.codebind.com/linux-tutorials/install-opengl-ubuntu-linux/ 。 如果这不起作用,请按照此操作 - https://gist.github.com/shamiul94/a632f7ab94cf389e08efd7174335df1c

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多