0

Sorry if this question is a bit long winded. I know there is a lot of ground already covered here.

Essentially I get the below error when including my tst_engine.lib in another target.

lld-link: error: undefined symbol: SDL_Init
>>> referenced by C:\Users\n10255460\CLionProjects\2D_engine_and_game\tst_engine\TstGameEngine.cpp:22
>>>               tst_engine.lib(TstGameEngine.cpp.obj):(public: __cdecl tst::TstGameEngine::TstGameEngine(bool *))

lld-link: error: undefined symbol: SDL_Quit
>>> referenced by C:\Users\n10255460\CLionProjects\2D_engine_and_game\tst_engine\TstGameEngine.cpp:43
>>>               tst_engine.lib(TstGameEngine.cpp.obj):(public: __cdecl tst::TstGameEngine::~TstGameEngine(void))
ninja: build stopped: subcommand failed.

As It says above, SDL is used in a .cpp file not a .h file. On top of this, this error goes away when including SDL as a dependency in the target I'm attempting to compile with tst_engine as a library.

As far as I've previously understood things, if a third party library is in a source file it should never become a dependency for those who use the lib associated with the source file. Below are the two relevant cmake files:

executable that triggers the error:

cmake_minimum_required(VERSION 3.24)
project(game)

set(CMAKE_CXX_STANDARD 20)

include_directories(../tst_engine)

set(lib ${CMAKE_SOURCE_DIR}\\libraries\\lib)

include_directories(${CMAKE_SOURCE_DIR}\\libraries\\include)

add_executable(game main.cpp)
target_link_libraries(game
        ${CMAKE_SOURCE_DIR}\\cmake-build-debug\\tst_engine\\tst_engine.lib
)

library that compiles successfully:

cmake_minimum_required(VERSION 3.24)
project(tst_engine)

set(CMAKE_CXX_STANDARD 20)

set(lib ${CMAKE_SOURCE_DIR}\\libraries\\lib)

include_directories(${CMAKE_SOURCE_DIR}\\libraries\\include)

add_library(tst_engine STATIC TstGameEngine.h TstGameEngine.cpp)
target_link_libraries(tst_engine PRIVATE ${lib}\\SDL2main.lib ${lib}\\SDL2.lib ${lib}\\SDL2-static.lib)
Jack Benson
  • 107
  • 1
  • 8
  • 1
    Cmake does not in any way "include" SDL into the static library. See [this possible dup](/questions/53672632/how-to-model-transitive-dependencies-between-static-libraries-in-cmake) – Botje Dec 20 '22 at 10:37
  • 2
    One static library does not link with another static library it depends on. Instead, the final target needs to link all the dependencies. You can tell CMake to pass the dependency by changing `PRIVATE` to `PUBLIC` in your lib's `target_link_libraries`. – Krzysiek Karbowiak Dec 20 '22 at 11:32
  • This may help: [https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake](https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake) – drescherjm Dec 20 '22 at 13:16
  • Just to mention, paths in CMake files can be written with the Posix style (no \\\) and `target_compile_features(tst_engine PUBLIC cxx_std_20)` and `target_link_directories` and `target_include_directories` are more modern than specific `set()` or `include_directories`... > 3.13 is called "more modern cmake". – Sandburg Dec 20 '22 at 13:20

0 Answers0