提问人:AlexZel 提问时间:12/17/2022 最后编辑:Rabbid76AlexZel 更新时间:12/18/2022 访问量:342
将 SDL3 项目未定义的引用链接到“__imp_glCreateShader”时出错
Error linking SDL3 project undefined reference to `__imp_glCreateShader'
问:
我有一个简单的测试项目。但是我无法使用 MinGW64 成功构建项目。 该项目由 CMakeLists.txt 和 main.cpp 组成
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
set(PROJECT "GLTest")
set(THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/3rd-party")
set(glm_INSTALL_DIR ${THIRD_PARTY_DIR}/glm)
set(sdl3_INSTALL_DIR ${THIRD_PARTY_DIR}/sdl3)
if (${PREBUILD_SOLUTION})
project(THIRD_PARTY NONE)
include(${THIRD_PARTY_DIR}/cmake/glm.cmake)
include(${THIRD_PARTY_DIR}/cmake/sdl3.cmake)
return()
else()
project(${PROJECT})
endif()
enable_language(C)
enable_language(CXX)
enable_language(ASM_NASM)
set(CMAKE_ASM_NASM_OBJECT_FORMAT win64)
set(CMAKE_ASM_NASM_COMPILE_OBJECT "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> \
<FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS off)
set(CMAKE_CXX_STANDARD_REQUIRED on)
if(MSVC)
#add_compile_options(/W4 /WX)
else()
#add_compile_options(-Wall -Wextra -Wpedantic )#-Werror)
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DGLTEST_DEBUG")
list(APPEND CMAKE_PREFIX_PATH "${THIRD_PARTY_DIR}/sdl3")
find_package(SDL3 REQUIRED SDL3)
find_package(OpenGL 4 REQUIRED OpenGL)
add_executable(${PROJECT} main.cpp)
target_include_directories(${PROJECT} PRIVATE
${SDL3_INCLUDE_DIR}
)
target_link_libraries(${PROJECT} PRIVATE
SDL3::SDL3-static
OpenGL::GL
OpenGL::GLU
)
main.cpp
#include <iostream>
#define GL_GLEXT_PROTOTYPES
#include <SDL3/SDL.h>
#include <SDL3/SDL_opengl.h>
typedef float t_mat4x4[16];
static inline void mat4x4_ortho( t_mat4x4 out, float left, float right, float bottom, float top, float znear, float zfar )
{
#define T(a, b) (a * 4 + b)
out[T(0,0)] = 2.0f / (right - left);
out[T(0,1)] = 0.0f;
out[T(0,2)] = 0.0f;
out[T(0,3)] = 0.0f;
out[T(1,1)] = 2.0f / (top - bottom);
out[T(1,0)] = 0.0f;
out[T(1,2)] = 0.0f;
out[T(1,3)] = 0.0f;
out[T(2,2)] = -2.0f / (zfar - znear);
out[T(2,0)] = 0.0f;
out[T(2,1)] = 0.0f;
out[T(2,3)] = 0.0f;
out[T(3,0)] = -(right + left) / (right - left);
out[T(3,1)] = -(top + bottom) / (top - bottom);
out[T(3,2)] = -(zfar + znear) / (zfar - znear);
out[T(3,3)] = 1.0f;
#undef T
}
static const char * vertex_shader =
"#version 130\n"
"in vec2 i_position;\n"
"in vec4 i_color;\n"
"out vec4 v_color;\n"
"uniform mat4 u_projection_matrix;\n"
"void main() {\n"
" v_color = i_color;\n"
" gl_Position = u_projection_matrix * vec4( i_position, 0.0, 1.0 );\n"
"}\n";
static const char * fragment_shader =
"#version 130\n"
"in vec4 v_color;\n"
"out vec4 o_color;\n"
"void main() {\n"
" o_color = v_color;\n"
"}\n";
typedef enum t_attrib_id
{
attrib_position,
attrib_color
} t_attrib_id;
int main( int argc, char * argv[] )
{
SDL_Init( SDL_INIT_VIDEO );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 4 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 5 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
static const int width = 800;
static const int height = 600;
SDL_Window * window = SDL_CreateWindow( "", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
SDL_GLContext context = SDL_GL_CreateContext( window );
GLuint vs, fs, program;
vs = glCreateShader( GL_VERTEX_SHADER );
fs = glCreateShader( GL_FRAGMENT_SHADER );
int length = strlen( vertex_shader );
glShaderSource( vs, 1, ( const GLchar ** )&vertex_shader, &length );
glCompileShader( vs );
GLint status;
glGetShaderiv( vs, GL_COMPILE_STATUS, &status );
if( status == GL_FALSE )
{
fprintf( stderr, "vertex shader compilation failed\n" );
return 1;
}
length = strlen( fragment_shader );
glShaderSource( fs, 1, ( const GLchar ** )&fragment_shader, &length );
glCompileShader( fs );
glGetShaderiv( fs, GL_COMPILE_STATUS, &status );
if( status == GL_FALSE )
{
fprintf( stderr, "fragment shader compilation failed\n" );
return 1;
}
program = glCreateProgram();
glAttachShader( program, vs );
glAttachShader( program, fs );
glBindAttribLocation( program, attrib_position, "i_position" );
glBindAttribLocation( program, attrib_color, "i_color" );
glLinkProgram( program );
glUseProgram( program );
glDisable( GL_DEPTH_TEST );
glClearColor( 0.5, 0.0, 0.0, 0.0 );
glViewport( 0, 0, width, height );
GLuint vao, vbo;
glGenVertexArrays( 1, &vao );
glGenBuffers( 1, &vbo );
glBindVertexArray( vao );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glEnableVertexAttribArray( attrib_position );
glEnableVertexAttribArray( attrib_color );
glVertexAttribPointer( attrib_color, 4, GL_FLOAT, GL_FALSE, sizeof( float ) * 6, 0 );
glVertexAttribPointer( attrib_position, 2, GL_FLOAT, GL_FALSE, sizeof( float ) * 6, ( void * )(4 * sizeof(float)) );
const GLfloat g_vertex_buffer_data[] = {
/* R, G, B, A, X, Y */
1, 0, 0, 1, 0, 0,
0, 1, 0, 1, width, 0,
0, 0, 1, 1, width, height,
1, 0, 0, 1, 0, 0,
0, 0, 1, 1, width, height,
1, 1, 1, 1, 0, height
};
glBufferData( GL_ARRAY_BUFFER, sizeof( g_vertex_buffer_data ), g_vertex_buffer_data, GL_STATIC_DRAW );
t_mat4x4 projection_matrix;
mat4x4_ortho( projection_matrix, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 100.0f );
glUniformMatrix4fv( glGetUniformLocation( program, "u_projection_matrix" ), 1, GL_FALSE, projection_matrix );
for( ;; )
{
glClear( GL_COLOR_BUFFER_BIT );
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYUP:
if( event.key.keysym.sym == SDLK_ESCAPE )
return 0;
break;
}
}
glBindVertexArray( vao );
glDrawArrays( GL_TRIANGLES, 0, 6 );
SDL_GL_SwapWindow( window );
SDL_Delay( 1 );
}
SDL_GL_DeleteContext( context );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
但是当我尝试构建一个项目时,我得到一个链接错误。我在互联网上到处看。但我还没有找到问题的答案。也许有人遇到过类似的问题?undefined reference to __imp_glCreateShader"
更新:
生成命令
[build] cmd.exe /C "cd . && F:\msys64\mingw64\bin\g++.exe -g -DGLTEST_DEBUG CMakeFiles/GLTest.dir/main.cpp.obj -o GLTest.exe -Wl,--out-implib,libGLTest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 ../3rd-party/sdl3/lib/libSDL3.dll.a -lopengl32 -lglu32 -lopengl32 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
错误信息
[build] cmd.exe /C "cd . && F:\msys64\mingw64\bin\g++.exe -g -DGLTEST_DEBUG CMakeFiles/GLTest.dir/main.cpp.obj -o GLTest.exe -Wl,--out-implib,libGLTest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 ../3rd-party/sdl3/lib/libSDL3.dll.a -lopengl32 -lglu32 -lopengl32 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/GLTest.dir/main.cpp.obj: in function `main':
[build] F:/VSCodeProject/GLTest/main.cpp:85: undefined reference to `__imp_glCreateShader'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:86: undefined reference to `__imp_glCreateShader'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:89: undefined reference to `__imp_glShaderSource'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:90: undefined reference to `__imp_glCompileShader'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:93: undefined reference to `__imp_glGetShaderiv'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:101: undefined reference to `__imp_glShaderSource'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:102: undefined reference to `__imp_glCompileShader'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:104: undefined reference to `__imp_glGetShaderiv'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:111: undefined reference to `__imp_glCreateProgram'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:112: undefined reference to `__imp_glAttachShader'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:113: undefined reference to `__imp_glAttachShader'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:115: undefined reference to `__imp_glBindAttribLocation'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:116: undefined reference to `__imp_glBindAttribLocation'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:117: undefined reference to `__imp_glLinkProgram'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:119: undefined reference to `__imp_glUseProgram'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:127: undefined reference to `__imp_glGenVertexArrays'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:128: undefined reference to `__imp_glGenBuffers'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:129: undefined reference to `__imp_glBindVertexArray'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:130: undefined reference to `__imp_glBindBuffer'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:132: undefined reference to `__imp_glEnableVertexAttribArray'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:133: undefined reference to `__imp_glEnableVertexAttribArray'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:135: undefined reference to `__imp_glVertexAttribPointer'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:136: undefined reference to `__imp_glVertexAttribPointer'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:149: undefined reference to `__imp_glBufferData'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:153: undefined reference to `__imp_glGetUniformLocation'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:153: undefined reference to `__imp_glUniformMatrix4fv'
[build] F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: F:/VSCodeProject/GLTest/main.cpp:171: undefined reference to `__imp_glBindVertexArray'
[build] collect2.exe: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[proc] The command: "F:\Program Files\CMake\bin\cmake.EXE" --build f:/VSCodeProject/GLTest/build --config Debug --target all -- exited with code: 1 and signal: null
[build] Build finished with exit code 1
答: 暂无答案
评论
mingw32-make
VERBOSE=1
opengl32
wglGetProcAddress
SDL_GL_GetProcAddress