【发布时间】:2018-06-07 04:22:58
【问题描述】:
我正在尝试在 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)
Applicatin.cpp(直接取自glfw.org here)
#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
【问题讨论】:
-
您的项目中没有文件
OpenGL/dependencies/GLFW/include/lib/libglfw3.a。你有OpenGL/dependencies/GLFW/lib/libglfw3.a一个。顺便说一句,为什么使用find_package(OpenGL)而不使用它的结果? -
@Tsyvarev 答案是我对 c++ 和 CMake 都是新手。我正在犯我没有注意到的菜鸟错误。请详细说明如何纠正我的错误。
-
如果你真的使用
#import而不是#include来包含OpenGL头,那肯定是不正确的。 -
@SeanBurton Id 宁愿做正确的事。我发现这是一个可能的解决方案,所以我尝试了它。我现在正在删除它,请告知链接 OpenGL 的正确方法
-
您需要使用 GL 加载器,例如 glad