0

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.

gexicide
  • 38,535
  • 21
  • 92
  • 152
  • "It looks like this picked up the .lib. But where is the path of the .dll specified?" - When **build** an executable/library, only `.lib` file is required and used. A file `.dll` is needed (and used) only at **runtime**, when you run the executable. – Tsyvarev Oct 31 '20 at 13:18
  • @Tsyvarev okay, that makes sense. But then I don't understand even more where the error comes from ;). – gexicide Oct 31 '20 at 13:54
  • Which SDL2 development package did you download? the VC one or the mingw one? Clang is [ABI-compatible with Visual studio](https://clang.llvm.org/docs/MSVCCompatibility.html) but not with MinGW. – Botje Oct 31 '20 at 14:11
  • @Botje: The VC one: SDL2-devel-2.0.12-VC.zip – gexicide Oct 31 '20 at 14:37
  • take a look at this thread which was created for the specific purpose of dealing with several SDL2 setup problems: https://stackoverflow.com/questions/64396979/how-do-i-use-sdl2-in-my-programs-correctly/64397689#64397689 – Ric Oct 31 '20 at 15:17
  • @Ric: Thanks, the thread helped indeed. My compiler was configured 32 bit, but the lib I used was 64 bit. – gexicide Oct 31 '20 at 15:31
  • @Botje It becomes mostly compatible with MinGW with a right `--target` flag. – HolyBlackCat Oct 31 '20 at 15:40

1 Answers1

1

I found the problem, thanks to the comment from @Ric: I was using a 32 bit compiler with the 64 bit version of the library.

gexicide
  • 38,535
  • 21
  • 92
  • 152