c++ - 尝试通过 Ubuntu 上的 CLion 将 GLFW 从源代码链接到 C++ 项目时出错
问题描述
我正在尝试将 GLFW 从源代码添加到我的项目中。我按照文件中的说明做了,但出现错误。
错误:
/usr/bin/ld:CMakeFiles/mancala_graphics.dir/main.cpp.o:在函数“main”中:
/path/to/clion/mancala_graphics/main.cpp:47:未定义对“glClear”的引用
collect2:错误:ld 返回 1 个退出状态
make[ 3 ]: *** [CMakeFiles/mancala_graphics.dir/build.make:88: mancala_graphics] 错误 1
make[1]: *** [CMakeFiles/Makefile2:115: CMakeFiles/mancala_graphics.dir/all] 错误 2
make[ 2 ]: *** [CMakeFiles/Makefile2:122: CMakeFiles/mancala_graphics.dir/rule] 错误 2
make: *** [Makefile:164: mancala_graphics] 错误 2
源代码如下及其来自文档:
#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", NULL, NULL);
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);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
CMake 在下面,这也来自文档:
cmake_minimum_required(VERSION 3.16)
project(mancala_graphics)
set(CMAKE_CXX_STANDARD 20)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(Dependencies/GLFW)
add_executable(mancala_graphics main.cpp)
target_link_libraries(mancala_graphics glfw)
正如您在图片中看到的那样,gl.h 中似乎定义了 glClear
当我转到 gl.h 时,它不在我下载并保存的源文件中,Dependencies/
但/usr/include/GL/gl.h
它可能是错误的根源,因为当我打开头文件时,我看到一条警告说该文件不属于任何项目你看:
问题是:这个配置有什么问题,为什么我不能运行文档中的代码片段?
还有一个问题是:如何将 GLFW 从源代码添加到我的项目中?
IDE:克莱恩
操作系统:Linux / Ubuntu
编辑
cmake_minimum_required(VERSION 3.16)
project(mancala_graphics)
set(CMAKE_CXX_STANDARD 20)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
find_package(OpenGL REQUIRED)
add_subdirectory(Dependencies/GLFW)
add_executable(mancala_graphics main.cpp)
target_link_libraries(mancala_graphics OpenGL::GL)
target_link_libraries(mancala_graphics glfw)
当我像上面的代码一样编写 cmake 时,但感觉有些不对劲。
解决方案
推荐阅读
- javascript - 使用带有 nuxt 的外部组件的问题
- c# - 为什么我的 wpf 应用程序内存分配稳步增加?
- apache - 我无法导航到 xampp 服务器中的本地主机
- tensorflow - 错误:没有用户提供的默认构造函数的 const 类型“const Subgraph::Identity”对象的默认初始化
- list - Netlogo:在代理列表中连接邻居列表的元素
- javascript - 2个组件几乎相同。唯一的区别是每个组件都有不同的 GET 请求。合并它们?
- apache-spark - 使用 ojdbc14.jar 在 spark-sql-2.4.1v 中将日期作为 lowerBound/upperBound 传递时引发 NumberFormatException?
- c# - 如何根据自定义属性从集合中动态创建对象?
- python - 为什么在列表的迭代过程中,当列表被删除时,for 循环会意外停止?
- wpf - 每次 5 秒后 MediaElement 冻结