I tried to use SDL2 on Windows, using the clang that comes with Visual Studio 2019, but I can't get it to link.
Here is a minimal example that I can't get to link.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(test VERSION 1.0)
# C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_VERBOSE_MAKEFILE ON)
# add the executable
add_executable(simpleui src/simpleui/simpleui.cpp)
# Link SDL2
add_library(SDL2 SHARED IMPORTED)
set_target_properties(SDL2 PROPERTIES
IMPORTED_LOCATION "C:/Program Files/SDL2-2.0.12/lib/x64/SDL2.dll"
IMPORTED_IMPLIB "C:/Program Files/SDL2-2.0.12/lib/x64/SDL2.lib"
LINKER_LANGUAGE C
)
target_include_directories(simpleui PUBLIC "C:\\Program Files\\SDL2-2.0.12\\include")
target_link_libraries(simpleui SDL2)
# subsystem
target_link_options(simpleui PUBLIC -Xlinker /subsystem:windows)
simpleui.cpp
#include <SDL.h>
#include <Windows.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
SDL_Init( SDL_INIT_VIDEO );
SDL_Quit();
return 0;
}
SDL2 is located at C:/Program Files/SDL2-2.0.12
Here is the error I get:
C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\Llvm\bin\CLANG_~1.EXE -fuse-ld=lld-link -nostartfiles -nostdlib -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -Xlinker /subsystem:windows @CMakeFiles\simpleui.dir\objects1.rsp -o simpleui.exe -Xlinker /implib:simpleui.lib -Xlinker /pdb:C:\repos\ice\cmake-build-debug\simpleui.pdb -Xlinker /version:0.0 @CMakeFiles\simpleui.dir\linklibs.rsp
lld-link: error: undefined symbol: _SDL_Init
>>> referenced by C:\repos\ice\src\simpleui\simpleui.cpp:8
>>> CMakeFiles/simpleui.dir/src/simpleui/simpleui.cpp.obj:(_wWinMain@16)
lld-link: error: undefined symbol: _SDL_Quit
>>> referenced by C:\repos\ice\src\simpleui\simpleui.cpp:9
>>> CMakeFiles/simpleui.dir/src/simpleui/simpleui.cpp.obj:(_wWinMain@16)
CLANG_~1: error: linker command failed with exit code 1 (use -v to see invocation)
So it seems it cannot link SDL. Here are the contents of @CMakeFiles\simpleui.dir\linklibs.rsp
:
"C:/Program Files/SDL2-2.0.12/lib/x64/SDL2.lib" -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames
It looks like this picked up the .lib
. But where is the path of the .dll
specified? Maybe that's the problem, since it cannot find the .dll
, but I have specified this in IMPORTED_LOCATION
.