I just started a simple SDL2 project. I am trying to set up the project as platform independent as possible.
I have googletest and SDL2 as submodules. They both come with their own CMakeLists file. After configuring as building gtest just works, finds the include files, finds the .lib, the tests folder builds and runs as expected. But I get and error that SDL2main.lib cannot be found. For release build a SDL2main.lib, for debug build a SDL2maind.lib is generated. The src/CMakeLists.txt below did not work for me.
How do I make it that it doesn't matter if I build for Windows or Linux, Release or Debug, it always finds the SDL lib and dll?
Project
|-.git
|- build/
|- src/
|- main.cpp
|- CMakeLists.txt
|- submodules/
|- googletest/
|- CMakeLists.txt
|- SDL/
|- CMakeLists.txt
|- tests
|- main.cpp
|- CMakeLists.txt
|- CMakeLists.txt
My root CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PROJECT_NAME MyProject)
set(PROJECT_TESTS_NAME MyProject_tests)
project(${PROJECT_NAME} VERSION 1.0.0)
add_subdirectory(submodules/googletest)
add_subdirectory(submodules/SDL)
include_directories(submodules/googletest/googletest/include/gtest)
include_directories(submodules/SDL/include)
include_directories(src)
add_subdirectory(src)
if(BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
My src/CMakeLists.txt:
add_executable(${PROJECT_NAME} main.cpp)
target_link_directories(${PROJECT_NAME} PUBLIC
"${CMAKE_BINARY_DIR}/submodules/SDL"
"${CMAKE_BINARY_DIR}/submodules/SDL/Debug"
"${CMAKE_BINARY_DIR}/submodules/SDL/Release")
if("${CMAKE_BUILD_TYPE_INIT}" STREQUAL Release)
target_link_libraries(${PROJECT_NAME} PUBLIC SDL2main)
else()
target_link_libraries(${PROJECT_NAME} PUBLIC SDL2maind)
endif()
My tests/CMakeLists.txt
include_directories(${PROJECT_SOURCE_DIR}/src)
add_executable(${PROJECT_TESTS_NAME} main.cpp)
target_link_libraries(${PROJECT_TESTS_NAME} PUBLIC gtest_main)