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