首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用CMake在Mac上连接静态GLFW和OpenGL

使用CMake在Mac上连接静态GLFW和OpenGL
EN

Stack Overflow用户
提问于 2018-06-07 12:22:58
回答 2查看 4.4K关注 0票数 0

我正在尝试在mac上测试glfw3。我不能建立一个简单的项目,因为我不能链接到OpenGL。

目录结构

代码语言:javascript
运行
复制
├── CMakeLists.txt
├── OpenGL
│   ├── Application.cpp
│   ├── CMakeLists.txt
│   └── dependencies
│       └── GLFW
│           ├── include
│           │   └── GLFW
│           │       ├── glfw3.h
│           │       └── glfw3native.h
│           ├── lib
│           │   └── libglfw3.a
│           └── src
└── bin

CMakeList.txt

顶级CMakeList.txt

代码语言:javascript
运行
复制
cmake_minimum_required(VERSION 3.11)

set(CMAKE_CXX_STANDARD 17)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

add_subdirectory(OpenGL)

嵌套的CMakeList.txt

代码语言:javascript
运行
复制
cmake_minimum_required(VERSION 3.11)
project(OpenGL)

find_package(OpenGL REQUIRED)

add_executable(OpenGL Application.cpp)

target_include_directories(OpenGL PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/GLFW/include)
target_link_libraries(OpenGL ${OPENGL_LIBRARIES} ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/GLFW/lib/libglfw3.a)

Applicatin.cpp (直接取自glfw.org Applicatin.cpp

代码语言:javascript
运行
复制
#include <iostream>
#include <GLFW/glfw3.h>

int main()
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", nullptr, nullptr);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT); <-- this line throws an error

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

错误消息

代码语言:javascript
运行
复制
make[3]: *** No rule to make target '../OpenGL/dependencies/GLFW/include/lib/libglfw3.a', needed by '../bin/OpenGL'.  Stop.
make[3]: *** Waiting for unfinished jobs....
[ 50%] Building CXX object OpenGL/CMakeFiles/OpenGL.dir/Application.cpp.o
/Users/dblock/CLionProjects/OpenGL/OpenGL/Application.cpp:27:17: error: use of undeclared identifier 'GL_COLOR_BUFFER_BIT'
        glClear(GL_COLOR_BUFFER_BIT);
                ^
1 error generated.
make[3]: *** [OpenGL/CMakeFiles/OpenGL.dir/build.make:63: OpenGL/CMakeFiles/OpenGL.dir/Application.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:86: OpenGL/CMakeFiles/OpenGL.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:98: OpenGL/CMakeFiles/OpenGL.dir/rule] Error 2
make: *** [Makefile:118: OpenGL] Error 2

我的集成开发环境(CLion)显示以下内容

代码语言:javascript
运行
复制
Can't resolve variable 'glClear'
Can't resolve variable 'GL_COLOR_BUFFER_BIT'

请告诉我哪里出错了。

编辑

我添加了#import,现在我得到一个新的错误

代码语言:javascript
运行
复制
/usr/local/Cellar/cmake/3.11.1/bin/cmake --build /Users/dblock/CLionProjects/OpenGL/cmake-build-debug --target OpenGL -- -j 4
make[3]: *** No rule to make target '../OpenGL/dependencies/GLFW/include/lib/libglfw3.a', needed by '../bin/OpenGL'.  Stop.
make[2]: *** [CMakeFiles/Makefile2:86: OpenGL/CMakeFiles/OpenGL.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:98: OpenGL/CMakeFiles/OpenGL.dir/rule] Error 2
make: *** [Makefile:118: OpenGL] Error 2

解决方案

代码语言:javascript
运行
复制
#include <OpenGL/gl.h> //<-- add this included to main.cpp fixed my issue
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-10 04:37:31

IDE清楚地表明没有找到glClearGL_COLOR_BUFFER_BIT的声明

由于OpenGL是一种标准/规范,因此要由驱动程序制造商来实现特定图形卡支持的驱动程序的规范。因为有许多不同版本的OpenGL驱动程序,所以它的大多数函数的位置在编译时是未知的,需要在运行时查询。然后,开发人员的任务是检索他/她需要的函数的位置,并将它们存储在函数指针中以供以后使用。

您需要声明OpenGL函数的头部和加载这些函数的加载器。值得庆幸的是,也有用于此目的的库,其中GLAD是一个流行的、最新的库。

生成GLAD /header- http://glad.dav1d.de

更新目录结构:

代码语言:javascript
运行
复制
├── CMakeLists.txt
├── OpenGL
│   ├── Application.cpp
│   ├── CMakeLists.txt
│   ├── glad.c
│   └── dependencies
│       ├── GLFW
│       │   └── ...
│       ├── glad
│       │   └── glad.h
│       └── KHR
│           └── khrplatform.h
└── bin

更新嵌套的CMakeList.txt:

代码语言:javascript
运行
复制
set(SOURCES Application.cpp glad.c)
add_executable(OpenGL ${SOURCES})
target_include_directories(OpenGL PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/dependencies)

更新Applicatin.cpp:

代码语言:javascript
运行
复制
#include <glad/glad.h>
#include <GLFW/glfw3.h>
...

glfwMakeContextCurrent(window);

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{ 
  // Failed to create GLFW window
  glfwTerminate();
  return -1;
}
// now you can use OpenGL functions

有关更多信息,请访问GLAD github,完成example。优秀的OpenGL tutorials

票数 3
EN

Stack Overflow用户

发布于 2018-06-07 12:56:11

我怀疑cmake解析到的相对路径不正确(不存在)。我建议您从HomeBrew安装glfw3,如下所示:

代码语言:javascript
运行
复制
brew install --static glfw3

并将glfw3的绝对路径指定为"/usr/local/lib“

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50732921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档