未定义符号:__declspec(dllimport)glewInit

jtoj6r0c  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(155)

我试图在Windows上使用CMake编译一个带有SDL 2和GLEW的程序。我没有使用Visual Studio。我以LLVM-MinGW的形式安装了MinGW,并使用WSL 2检查了此代码。
CMakeLists.txt

cmake_minimum_required(VERSION 3.23)
#set(CMAKE_VERBOSE_MAKEFILE ON)

project(hello)

set(CMAKE_C_STANDARD 11)

file(GLOB SOURCES src/*c)
add_executable(${PROJECT_NAME} ${SOURCES})

find_package(SDL2 REQUIRED)
find_package(GLEW REQUIRED GLEW_USE_STATIC_LIBS)
find_package(OpenGL REQUIRED)

include_directories(${CMAKE_SOURCE_DIR}/lib)

target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Werror -std=c11)
# Using -v with lld to deduce errors
target_link_options(${PROJECT_NAME} PRIVATE -v)

# Neither of the below work:
target_link_libraries(${PROJECT_NAME} ${GLEW_STATIC_LIBRARIES})
#target_link_libraries(${PROJECT_NAME} ${GLEW_LIBRARIES})

target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})

include_directories(${GLEW_INCLUDE_DIRS})
include_directories(${OPENGL_INCLUDE_DIR})
include_directories(${SDL2_INCLUDE_DIRS})

main.h

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

如果我把glew.h放在SDL_opengl.h之后,就会出现大量的“未知类型名称”等错误。所以,我想它的位置是正确的。此外,我已经尝试了与#define GLEW_STATIC之前的一切。
以下是make的输出(在成功执行cmake ..之后):

[ 50%] Linking C executable hello.exe
clang version 17.0.1 (https://github.com/llvm/llvm-project.git e19b7dc36bc047b9eb72078d034596be766da350)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/llvm-mingw/bin
 "C:/llvm-mingw/bin/ld.lld" -m i386pep -Bdynamic -o hello.exe C:/llvm-mingw/x86_64-w64-mingw32/lib/crt2.o C:/llvm-mingw/x86_64-w64-mingw32/lib/crtbegin.o -LC:/llvm-mingw/x86_64-w64-mingw32/lib -LC:/llvm-mingw/x86_64-w64-mingw32/mingw/lib -LC:/llvm-mingw/lib -LC:/llvm-mingw/lib/clang/17/lib/windows --whole-archive "CMakeFiles\\hello.dir/objects.a" --no-whole-archive --out-implib libhello.dll.a --major-image-version 0 --minor-image-version 0 "C:/Program Files/GLEW/lib/glew32s.lib" "C:/Program Files (x86)/SDL2/lib/libSDL2main.a" "C:/Program Files (x86)/SDL2/lib/libSDL2.dll.a" -lshell32 --undefined=WinMain -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -lmingw32 C:/llvm-mingw/lib/clang/17/lib/windows/libclang_rt.builtins-x86_64.a -lunwind -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 C:/llvm-mingw/lib/clang/17/lib/windows/libclang_rt.builtins-x86_64.a -lunwind -lmoldname -lmingwex -lmsvcrt -lkernel32 C:/llvm-mingw/x86_64-w64-mingw32/lib/crtend.o
ld.lld: error: undefined symbol: glewInit
>>> referenced by objects.a(main.c.obj):(SDL_main)

ld.lld: error: undefined symbol: glewGetErrorString
>>> referenced by objects.a(main.c.obj):(SDL_main)
clang-17: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles\hello.dir\build.make:102: hello.exe] Error 1
make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/hello.dir/all] Error 2
make: *** [Makefile:90: all] Error 2

然而,值得注意的是,代码在WSL 2(Ubuntu 22.04 LTS)上编译得很好。输出量:

[ 50%] Building C object CMakeFiles/hello.dir/src/main.c.o
[100%] Linking C executable hello
Using built-in specs.
COLLECT_GCC=/usr/bin/cc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/11/:/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/11/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-L/usr/lib/x86_64-linux-gnu' '-mtune=generic' '-march=x86-64' '-dumpdir' 'hello.'
 /usr/lib/gcc/x86_64-linux-gnu/11/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/11/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper -plugin-opt=-fresolution=/tmp/ccDMHV7r.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro -o hello /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/11/crtbeginS.o -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/11 -L/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/11/../../.. CMakeFiles/hello.dir/src/main.c.o /usr/lib/x86_64-linux-gnu/libGLEW.a /usr/lib/x86_64-linux-gnu/libOpenGL.so /usr/lib/x86_64-linux-gnu/libGLX.so /usr/lib/x86_64-linux-gnu/libGLU.so -lSDL2 -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/11/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-L/usr/lib/x86_64-linux-gnu' '-mtune=generic' '-march=x86-64' '-dumpdir' 'hello.'
[100%] Built target hello

乍一看,我会说clang是问题所在。然而,这是LLVM-MinGW附带的,使用旧版本的MinGW(带gcc)使我无法使用SDL 2。所以,首先,问题是什么?那么,如果问题是clang,我如何使用LLVM-MinGW(与SDL 2一起工作)获得gcc?

yqkkidmi

yqkkidmi1#

您需要将库放在命令行的末尾。它们位于对象文件之间。这行不通。
库只解析一次,即使它们包含该符号,如果需要它的目标文件放在库之后,库将不会再次搜索,您将得到未定义的符号错误。

相关问题