You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译流程中哪一步导致x64 Release构建调用GLEW OpenGL函数崩溃?

x64 Release模式下OpenGL静态库调用GLEW函数崩溃问题

我正在开发一个简化OpenGL二维渲染的静态库,基于GLEW和OpenGL构建。问题出在链接该库的可执行文件上:x64 Release模式构建时,首次调用Windows默认OpenGL实现之外的GLEW定义函数就会崩溃,但x64 Debug模式运行完全正常。

崩溃错误信息:

Exception thrown at 0x0000000000000000 in ExampleHelloWorld.exe: 0xC0000005: Access violation executing location 0x0000000000000000.

使用环境:Microsoft Visual Studio 2022 跨平台CMake项目类型。

以下是可复现问题的最简代码:

CMakeLists.txt

cmake_minimum_required (VERSION 3.15)
project("ReproducingOpenGLErrors")
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED true)
set(glew_DIR "${CMAKE_CURRENT_LIST_DIR}/cmake") 
find_package(glew REQUIRED)
find_package(OpenGL REQUIRED)

# -----LIBRARY BUILD-----

add_library(ReproducingOpenGLErrors STATIC Library.c)
target_include_directories(ReproducingOpenGLErrors PUBLIC Library.h ${glew_INCLUDE_DIRS} OpenGL::GL OpenGL::GLU)

# -----EXAMPLE BUILD-----

set(glfw3_DIR "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(glfw3 REQUIRED)
add_executable(ExampleHelloWorld Main.c)
target_include_directories(ExampleHelloWorld PUBLIC ${glew_INCLUDE_DIRS} ${glfw3_INCLUDE_DIRS} OpenGL::GL OpenGL::GLU)
target_link_libraries(ExampleHelloWorld ${glew_LIBRARIES} ${glfw3_LIBRARIES} ReproducingOpenGLErrors ${OPENGL_LIBRARIES})

glew-config.cmake

set(glew_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/include")
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
    set(glew_LIBRARIES "${PROJECT_SOURCE_DIR}/lib/glew32s.lib")
endif ()
string(STRIP "${glew_LIBRARIES}" glew_LIBRARIES)

glfw3-config.cmake

set(glfw3_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/include")
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
    set(glfw3_LIBRARIES "${PROJECT_SOURCE_DIR}/lib/glfw3_mt.lib")
endif ()
string(STRIP "${glfw3_LIBRARIES}" glfw3_LIBRARIES)

Library.h

#ifndef LIBRARY_H
#define LIBRARY_H
#ifdef __cplusplus
extern "C" {
#endif
#define GLEW_STATIC
#include "glew.h"
#include <gl/GL.h>
#include <gl/GLU.h>
GLuint CreateVertexShader(void);
#ifdef __cplusplus
}
#endif
#endif

Library.c

#include "Library.h"
GLuint CreateVertexShader(void) { return glCreateShader(GL_VERTEX_SHADER); }

Main.c

#include "Library.h"
#include "glfw3.h"
#include <assert.h>
int main(int argc, char** argv)
{
    assert(glfwInit());
    GLFWwindow* window = glfwCreateWindow(800, 600, "Hello World", NULL, NULL);
    assert(window != NULL);
    glfwMakeContextCurrent(window);
    assert(glewInit() == GLEW_OK);
    GLuint newShader = CreateVertexShader();
    return 0;
}

已尝试的解决方法(均无效):

  • 确保所有指针解引用前非空
  • 检查所有变量已显式初始化
  • 以C调用约定构建库
  • 尝试将静态库与GLEW链接

恳请帮忙排查编译环节的问题。


内容的提问来源于stack exchange,提问作者Sunshy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:32:04