0

I'm trying to learn modern c++ and already struggling with cmake to get an SDL project compiling. I'm using Windows with the MSVC toolchain, CLion as my IDE, and I've based my setup on several different blogs but the current on is based on https://trenki2.github.io/blog/2017/06/02/using-sdl2-with-cmake/.

I've taken their FindSDL2.cmake and placed it in my project's cmake folder. I then updated my CMakeLists.txt to be:

cmake_minimum_required(VERSION 3.21)
project(pikuma)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(pikuma WIN32 src/main.cpp)

message("TEST ${SDL2_LIBRARIES}")
target_link_libraries(pikuma ${SDL2_LIBRARIES})

# Copy assets on build
add_custom_command(
        TARGET pikuma PRE_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/assets
        ${CMAKE_CURRENT_BINARY_DIR}/assets
)

My main.cpp is exactly what's in the blog. When CMake runs it shows the following output:

C:\Users\me\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\213.6461.75\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=C:/Users/me/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/213.6461.75/bin/ninja/win/ninja.exe -G Ninja C:\Users\me\code\pikuma
TEST C:/sdl2;C:/sdl2/lib/x86/SDL2main.lib
-- Configuring done
WARNING: Target "pikuma" requests linking to directory "C:/sdl2".  Targets may link only to libraries.  CMake is dropping the item.
-- Generating done
-- Build files have been written to: C:/Users/me/code/pikuma/cmake-build-debug

[Finished]

The TEST line seems to imply that it's finding my SDL2 download, which I did place into c:\sdl2.

When I go to build my project I get the following error:

====================[ Build | pikuma | Debug ]==================================
C:\Users\me\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\213.6461.75\bin\cmake\win\bin\cmake.exe --build C:\Users\me\code\pikuma\cmake-build-debug --target pikuma
[1/2] Building CXX object CMakeFiles\pikuma.dir\src\main.cpp.obj
[2/2] Linking CXX executable pikuma.exe
FAILED: pikuma.exe 
cmd.exe /C "cmd.exe /C "cd /D C:\Users\me\code\pikuma\cmake-build-debug && C:\Users\me\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\213.6461.75\bin\cmake\win\bin\cmake.exe -E copy_directory C:/Users/me/code/pikuma/assets C:/Users/me/code/pikuma/cmake-build-debug/assets && cd C:\Users\me\code\pikuma\cmake-build-debug" && C:\Users\me\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\213.6461.75\bin\cmake\win\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\pikuma.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\mt.exe --manifests  -- C:\PROGRA~2\MICROS~2\2019\BUILDT~1\VC\Tools\MSVC\1429~1.301\bin\Hostx86\x86\link.exe /nologo CMakeFiles\pikuma.dir\src\main.cpp.obj  /out:pikuma.exe /implib:pikuma.lib /pdb:pikuma.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:windows  C:\sdl2\lib\x86\SDL2main.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~2\MICROS~2\2019\BUILDT~1\VC\Tools\MSVC\1429~1.301\bin\Hostx86\x86\link.exe /nologo CMakeFiles\pikuma.dir\src\main.cpp.obj /out:pikuma.exe /implib:pikuma.lib /pdb:pikuma.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:windows C:\sdl2\lib\x86\SDL2main.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\pikuma.dir/intermediate.manifest CMakeFiles\pikuma.dir/manifest.res" failed (exit code 1120) with the following output:
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_CreateWindow referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_DestroyWindow referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_CreateRenderer referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_SetRenderDrawColor referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_RenderClear referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_RenderPresent referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_Delay referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_Init referenced in function _SDL_main
main.cpp.obj : error LNK2019: unresolved external symbol _SDL_Quit referenced in function _SDL_main
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol _SDL_free referenced in function _main_getcmdline
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol _SDL_wcslen referenced in function _main_getcmdline
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol _SDL_strlen referenced in function _main_getcmdline
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol _SDL_iconv_string referenced in function _main_getcmdline
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol _SDL_SetMainReady referenced in function _main_getcmdline
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol _SDL_ShowSimpleMessageBox referenced in function _main_getcmdline
pikuma.exe : fatal error LNK1120: 15 unresolved externals
ninja: build stopped: subcommand failed.

It seems to be linking against sdl2 fine (as far as I can tell) as it's using the correct location of SDL2main.lib. I can't figure out what I'm doing wrong. Even using that blog posts exact same setup seems to have the same errors.c

KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • 1
    By the way, you can safely disregard any resource that encourages you to use `include_directories` or `target_link_libraries` without a visibility specifier (i.e. PRIVATE, PUBLIC, or INTERFACE). You should also avoid linking to anything besides an imported target (so with `::` in the name). Avoid expanding package variables (like `SDL2_INCLUDE_DIRS`) at all. – Alex Reinking Jan 23 '22 at 03:29
  • 1
    Also avoid using 3rd party find modules when the first party provides one. SDL2 comes with its own CMake config these days. – Alex Reinking Jan 23 '22 at 03:31
  • Looks like your `SDL2_LIBRARIES` is `C:/sdl2;C:/sdl2/lib/x86/SDL2main.lib`; this is incorrect as it have directory name instead of a path to `SDL2.lib`. – keltar Jan 23 '22 at 04:55
  • @keltar - that is almost certainly the fault of the bogus `FindSDL2.cmake` file discussed in the blog post. Attempting to make it work is unproductive... @KallDrexx should use SDL2's upstream CMake support. – Alex Reinking Jan 23 '22 at 05:30
  • 1
    @AlexReinking or a bad copypaste, as I fail to see how said file could substitute directory name instead of a library path. Or actual cmake script used is different (e.g. some other `Find*` or `config` cmake script in cmake search path). Or something else entirely which we don't see. SDL2 prebuilt archive for MSVC don't seem to include cmake configuration script. Whatever KallDrexx should do is for them to decide. – keltar Jan 23 '22 at 06:08
  • 1
    @AlexReinking Thanks. Vcpkg got me closer, and I wasn't aware SDL2 had an official one, as most google searches say to use some FindSDL2 equivalent. I still seem to be getting one last unresolved symbol that I can't quite figure out how to get rid of. – KallDrexx Jan 23 '22 at 13:01
  • 1
    Nevermind, figured that out, thanks the link you posted was the answer. – KallDrexx Jan 23 '22 at 13:03

0 Answers0