使用 CMake 在 Mac 上链接静态 GLFW 和 OpenGL
Linking Static GLFW and OpenGL on a Mac with CMake
我正在尝试在Mac上测试glfw3。我无法构建一个简单的项目,因为我无法链接到OpenGL。
目录结构
├── CMakeLists.txt
├── OpenGL
│ ├── Application.cpp
│ ├── CMakeLists.txt
│ └── dependencies
│ └── GLFW
│ ├── include
│ │ └── GLFW
│ │ ├── glfw3.h
│ │ └── glfw3native.h
│ ├── lib
│ │ └── libglfw3.a
│ └── src
└── bin
CMakeList.txt
顶级CMakeList.txt
cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_STANDARD 17)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
add_subdirectory(OpenGL)
嵌套的CMakeList.txt
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)
应用素.cpp(直接取自此处 glfw.org(
#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;
}
错误信息
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
我的IDE(CLion(说以下内容
Can't resolve variable 'glClear'
Can't resolve variable 'GL_COLOR_BUFFER_BIT'
请让我知道我哪里出错了。
编辑
我添加了 #import,现在我收到一个新错误
/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
溶液
#include <OpenGL/gl.h> //<-- add this included to main.cpp fixed my issue
IDE 清楚地表明没有找到glClear
和GL_COLOR_BUFFER_BIT
的声明
由于 OpenGL 是标准/规范,因此由驱动程序制造商将规范实现到特定显卡支持的驱动程序。由于 OpenGL 驱动程序有许多不同的版本,因此其大多数函数的位置在编译时是未知的,需要在运行时查询。然后,开发人员的任务是检索他/她需要的函数的位置,并将它们存储在函数指针中以供以后使用。
您需要声明 OpenGL 函数的标头和加载这些函数的加载器。值得庆幸的是,也有用于此目的的库,其中 GLAD 是一个流行且最新的库。
生成 GLAD 加载程序/标头 - http://glad.dav1d.de。
更新目录结构:
├── CMakeLists.txt
├── OpenGL
│ ├── Application.cpp
│ ├── CMakeLists.txt
│ ├── glad.c
│ └── dependencies
│ ├── GLFW
│ │ └── ...
│ ├── glad
│ │ └── glad.h
│ └── KHR
│ └── khrplatform.h
└── bin
更新嵌套的 CMakeList.txt:
set(SOURCES Application.cpp glad.c)
add_executable(OpenGL ${SOURCES})
target_include_directories(OpenGL PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/dependencies)
更新应用程序.cpp:
#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,完整示例。优秀的OpenGL教程。
我怀疑 cmake 解析到的相对路径不正确(不存在(。 我建议你像这样从HomeBrew安装glfw3:
brew install --static glfw3
并将 glfw3 的绝对路径指定为 "/usr/local/lib">
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- #为""定义宏;静态";针对不同的上下文
- cmake如何在fedora工作站中找到boost静态库包
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- cmake在我的项目中所需的所有静态库都不成功
- C++从另一个类访问公共静态向量的正确方法是什么
- 基于boost的程序的静态链接——zlib问题
- 在静态库中嵌入类方法
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 如何在C++中获得"静态纯虚拟"功能?
- 私有类型的静态常量成员
- 使用gcc从静态链接的文件中查找可选符号
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 如何在C++中使用非静态成员函数作为回调函数
- 将静态库链接到不带-fPIC的共享库中
- 静态结构和一个定义规则
- 为什么虚函数不能是静态的和全局的?
- 使用 CMake 在 Mac 上链接静态 GLFW 和 OpenGL
- Visual Studio 2013 C++链接虚拟机上的静态库glew、glfw