undefined symbol: __declspec(dllimport) glewInit

416 Views Asked by At

I am attempting to compile a program with SDL2 and GLEW using CMake on Windows. I am not using Visual Studio. I have MinGW installed in the form of LLVM-MinGW, and I have checked this code using WSL2.

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>

If I put glew.h after SDL_opengl.h, there are loads of errors with "unknown type name," etc. So, I'm thinking it's placed correctly. Also, I have tried with and without #define GLEW_STATIC before everything.

Here is the output of make (after successful 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

Notably, however, the code compiles just fine on WSL2 (Ubuntu 22.04 LTS). Output:

[ 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

At first guess, I would say that clang is the issue. However, it's what came with LLVM-MinGW, and using an older version of MinGW (with gcc) makes SDL2 fail for me. So, first, what's the issue? Then, if the issue is clang, how can I get gcc with LLVM-MinGW (which works with SDL2)?

1

There are 1 best solutions below

1
0___________ On

You need to place the library at the end of the command line. They are in the middle between object files. It will not work.

Libraries are parsed only once and even if they contain that symbol if object file needing it is placed after the library, the library will not be searched again and you will get the undefined symbol error.