带有 cmake 和 glew glfw3 和 glm 库的 Opengl

Posted

技术标签:

【中文标题】带有 cmake 和 glew glfw3 和 glm 库的 Opengl【英文标题】:Opengl with cmake and glew glfw3 and glm libraries 【发布时间】:2014-03-10 23:55:04 【问题描述】:

我在 Ubuntu 12.04 LTS 上编译 this example 时遇到问题:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <glm/glm.hpp>

using namespace glm;

int main()
if( !glfwInit() )

    fprintf( stderr, "Failed to initialize GLFW\n" );
    return -1;


glfwWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL

// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL )
    fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
    glfwTerminate();
    return -1;

glfwMakeContextCurrent(window);

// Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) 
    fprintf(stderr, "Failed to initialize GLEW\n");
    return -1;


glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

do
    // Draw nothing, see you in tutorial 2 !

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

 // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );

return 0;

我尝试在终端中使用带有正确标志的单行来编译它,但总是发现错误。在几个论坛中,我发现使用 cmake 查找、链接和编译我需要的库更好,所以使用this example 我尝试编写自己的 cmake 列表,获得以下代码:

cmake_minimum_required(VERSION 2.8)


#Encontrando y linkeando GLEW
find_package(GLEW REQUIRED)
include_directories($GLEW_INCLUDE_DIRS)
link_directories($GLEW_LIBRARY_DIRS)
add_definitions($GLEW_DEFINITIONS)
if(NOT GLEW_FOUND)
 message(Error " GLEW not found")
endif(NOT GLEW_FOUND)

#Encontrando y linkeando glfw3
find_package(GLFW REQUIRED)
include_directories($GLFW_INCLUDE_DIRS)
link_directories($GLFW_LIBRARY_DIRS)
add_definitions($GLFW_DEFINITIONS)

if(NOT GLFW_FOUND)
        message(Error "GLFW not found")
endif(NOT GLFW_FOUND)

#Encontrando y linkeando glm

find_package(GLM REQUIRED)
include_directories($GLM_INCLUDE_DIRS)
link_directories($GLM_LIBRARY_DIRS)
add_definitions($GLM_DEFINITIONS)

if(NOT GLM_FOUND)
        message(Error "GLM not found")
endif(NOT GLM_FOUND)

find_package(OpenGL REQUIRED)
include_directories($OpenGL_INCLUDE_DIRS)
link_directories($OpenGL_LIBRARY_DIRS)
add_definitions($OpenGL_DEFINITIONS)

if(NOT OpenGL_FOUND)
        message(Error "OpenGL not found")
endif(NOT OpenGL_FOUND)

#Incluir archivos

add_executable(abrir main.cpp)

target_link_libraries(abrir $OPENGL_LIBRARIES $GLEW_LIBRARIES $GLFW_LIBRARIES $GLM_LIBRARIES)

但我收到以下错误:

Could not find module FindGLEW.cmake or a configuration file for package GLEW.

Adjust CMAKE_MODULE_PATH to find FindGLEW.cmake or set GLEW_DIR to the
directory containing a CMake configuration file for GLEW.  The file will
have one of the following names:

GLEWConfig.cmake
glew-config.cmake



Error GLEW not found
CMake Error at CMakeLists.txt:14 (find_package):
Could not find module FindGLFW.cmake or a configuration file for package
GLFW.

Adjust CMAKE_MODULE_PATH to find FindGLFW.cmake or set GLFW_DIR to the
directory containing a CMake configuration file for GLFW.  The file will
have one of the following names:

GLFWConfig.cmake
glfw-config.cmake



ErrorGLFW not found
CMake Error at CMakeLists.txt:25 (find_package):
Could not find module FindGLM.cmake or a configuration file for package
GLM.

Adjust CMAKE_MODULE_PATH to find FindGLM.cmake or set GLM_DIR to the
directory containing a CMake configuration file for GLM.  The file will
have one of the following names:

GLMConfig.cmake
glm-config.cmake

如何解决这些错误?或者有没有更简单的方法来解决这个问题?

【问题讨论】:

你试过了吗...寻找 glew-config.cmake/glfw-config.cmake 等并在运行 cmake 之前相应地设置 CMAKE_MODULE_PATH? 【参考方案1】:

我知道这有点晚了。但是如果其他人遇到了这个问题。这就是我所做的。

我将此文件https://github.com/Groovounet/glm-deprecated/blob/master/util/FindGLM.cmake 复制到我的计算机中,即 /home/user/Libs/cmake/FindGLM.cmake

然后我将这一行添加到我的 CMakeLists.txt 中:

set(CMAKE_MODULE_PATH /home/user/Libs/cmake)

所以我文件的前 3 行是:

cmake_minimum_required (VERSION 2.6)
project (test)
set(CMAKE_MODULE_PATH /home/user/Libs/cmake)

然后我运行 cmake 和 make,我没有错误。

【讨论】:

这适用于那些试图编译 Arrayfire 库并收到 glm CMake 错误的人【参考方案2】:

我强烈建议您在该特定教程中使用 Qt Creator。至少,我是这样做的。为这些事情使用 IDE 总是更好。我记得我必须重写 CMakeLists 文件的某些部分才能将其正确构建到我的环境中(例如,我不想使用 AntTweakBar)。

该教程提供的 Qt Creator 说明非常好,它们应该对您有用。

另外,请记住安装您正在使用的教程中提到的每个软件包,包括 CMake。如果我没记错的话,它们应该可以正常工作:)

【讨论】:

以上是关于带有 cmake 和 glew glfw3 和 glm 库的 Opengl的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 GLFW3 初始化 GLew

使用 CMake 和 Visual Studio 2019 编译 GLEW?

GLEW 不适用于 CMAKE

GLFW3 中是不是有 glutDisplayFunc 等价物?

OSX Yosemite 10.10.3 上的 Cmake - GLEW:找不到包“gl”

GLEW + cmake 链接失败“未定义对符号 glDrawElements 的引用”+“命令行中缺少 DSO”