我试图在MacOS上的CLion中安装OpenGL。我的程序可以编译,但当我运行它时,我得到了这个crashreport.它和glfw网站上的代码不完全一样。
Time Awake Since Boot: 6200 seconds
System Integrity Protection: enabled
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [1694]
VM Regions Near 0:
-->
__TEXT 0000000109550000-000000010957c000 [ 176K] r-x/r-x SM=COW /Users/USER/Documents/*
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 ??? 000000000000000000 0 + 0
1 SandBoxGL 0x000000010955223a main + 362 (main.cpp:30)
2 libdyld.dylib 0x00007fff6765acc9 start + 1
Thread 1:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 2:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 3:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 4:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 5:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 6:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 7:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 8:
0 libsystem_pthread.dylib 0x00007fff6785ab68 start_wqthread + 0
Thread 9:: com.apple.NSEventThread
0 libsystem_kernel.dylib 0x00007fff6779be36 semaphore_wait_trap + 10
1 libdispatch.dylib 0x00007fff67601aed _dispatch_sema4_wait + 16
2 libdispatch.dylib 0x00007fff67601fbf _dispatch_semaphore_wait_slow + 98
3 com.apple.HIToolbox 0x00007fff2c35b940 _BeginEventReceiptOnThread + 159
4 com.apple.AppKit 0x00007fff2ab32555 _NSEventThread + 37
5 libsystem_pthread.dylib 0x00007fff6785f109 _pthread_start + 148
6 libsystem_pthread.dylib 0x00007fff6785ab8b thread_start + 15
Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x000000010957d350 rbx: 0x0000000000000000 rcx: 0x00007fbe2b8090ff rdx: 0x0000000000000002
rdi: 0x00007fbe2ae0eb40 rsi: 0x00007fbe2af78610 rbp: 0x00007ffee66af8f0 rsp: 0x00007ffee66af898
r8: 0x00007fff8e186e30 r9: 0x0000000000000010 r10: 0x00007ffee66afa38 r11: 0x0000000109600b46
r12: 0x0000000000000000 r13: 0x0000000000000000 r14: 0x0000000000000000 r15: 0x0000000000000000
rip: 0x0000000000000000 rfl: 0x0000000000010202 cr2: 0x0000000000000000
Logical CPU: 4
Error Code: 0x00000014 (no mapping for user instruction read)
Trap Number: 14
...(binary)
这和glfw网站上的代码不完全一样,我不得不导入glad,在第三行添加了宏和一些打印。网站上的代码甚至没有编译。就是这样的代码。
#include <iostream>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/glad.h>
int main(void)
{
if (!glfwInit()) {
return -1;
}
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
std::cout << "window: " << window;
if (!window)
{
std::cout << "problems with window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
我用HomeBrew安装了glfw库 还有在这个网站上下载的Glad库。https:/glad.dav1d.de。 与gl 4.1版本和所有扩展。我把所有的zip文件夹添加到 libs/glad
文件夹,并将其链接到我的项目中的 CMakeLists.txt
文件。这就是 CMakeLists.txt
文件。
cmake_minimum_required(VERSION 3.16)
project(SandBoxGL)
set(SRC_FILES
src/main.cpp
)
set(HEADER_FILES)
find_package(glfw3 REQUIRED)
find_package(OpenGl REQUIRED)
add_library(glad "libs/glad/src/glad.c")
include_directories("libs/glad/include")
set(LIBS glfw glad)
add_executable(SandBoxGL ${HEADER_FILES} ${SRC_FILES})
include_directories(
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/include"
)
target_link_libraries(${PROJECT_NAME} ${LIBS})
当我在CLion的调试器中运行它时,它停在了 glClear(GL_COLOR_BUFFER_BIT);
函数,并给我那个错误信息 EXC_BAD_ACCESS (code=1, address=0x0)
. 当我删除这一行时,我得到了一个黑色的窗口,如预期。有谁知道如何解决这个问题?
有可能是GLAD没有启动。试着添加这样的东西。
//Previous stuff
glfwMakeContextCurrent(window);
int gladErr = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); //Starts up GLAD
assert(gladErr != 0); //Checks glad is ok
if(GLAD_GL_VERSION_4_1){ //Makes sure OpenGL 4.1 is supported (not really necessary with window hints)
std::cout << "4.1 OK" << std::endl;
}
while (!glfwWindowShouldClose(window))
//Render loop