gpt4 book ai didi

c++ - 学习 OpenGL 时找不到 glClear

转载 作者:行者123 更新时间:2023-11-30 03:33:52 28 4
gpt4 key购买 nike

我正在尝试学习 OpenGL。我将 Clion 用作 IDE,它使用 CMakeLists.txt 文件来组织/编译项目。

由于某种原因,编译器找不到 glClear:

Undefined symbols for architecture x86_64:
"_glClear", referenced from:
_main in main.cpp.o
"_glClearColor", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [GraphicsPractice] Error 1
make[1]: *** [CMakeFiles/GraphicsPractice.dir/all] Error 2
make: *** [all] Error 2

我能够成功链接 GLEWGLFW,但是,当我调用 glClear(GL_COLOR_BUFFER_BIT) 时,我的代码中断了。我不确定为什么。如果有人能帮助我指出正确的方向,那就太好了。

CMakeList.txt

cmake_minimum_required(VERSION 3.6)
project(GraphicsPractice)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES src/main.cpp)

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(deps/glfw)

find_package(OpenGl REQUIRED)
find_package(GLEW REQUIRED)

include_directories("deps/glfw/include/")

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

target_link_libraries(${PROJECT_NAME} glfw glew)

main.cpp

#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main() {

glfwInit();

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", 0, nullptr);

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();


while (!glfwWindowShouldClose(window)) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);

glfwSwapBuffers(window);
glfwPollEvents();
}

glfwTerminate();
return 0;
}

最佳答案

您需要链接 OpenGL 库。即:

target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} glfw glew 等...)

关于c++ - 学习 OpenGL 时找不到 glClear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com