0

Please note that I'm new to CMake. How to link several static libraries(.lib) to my static library project which is initially created in Visual Studio 2019? I've used target_link+libraries and target_link_directories, and it doesn't work on the Static Library project but others.

Here is my script

target_link_libraries(${PROJECT_NAME} PUBLIC
    "SDL2;"
    "SDL2_image;"
    "SDL2_mixer;"
    "SDL2_ttf;"
    "zlibstat;"
    "Lua;"
    "box2d"
)

target_link_directories(${PROJECT_NAME} 
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/Source/Middleware/SDL2/lib/${CMAKE_VS_PLATFORM_NAME}/
        ${CMAKE_CURRENT_SOURCE_DIR}/Source/Middleware/SDL2_image/lib/${CMAKE_VS_PLATFORM_NAME}/
        ${CMAKE_CURRENT_SOURCE_DIR}/Source/Middleware/SDL2_mixer/lib/${CMAKE_VS_PLATFORM_NAME}/
        ${CMAKE_CURRENT_SOURCE_DIR}/Source/Middleware/SDL2_ttf/lib/${CMAKE_VS_PLATFORM_NAME}/
        ${CMAKE_CURRENT_SOURCE_DIR}/Source/Middleware/zlib/lib/${CMAKE_VS_PLATFORM_NAME}_$<CONFIG>/
        ${CMAKE_CURRENT_SOURCE_DIR}/Source/Middleware/Lua/lib/${CMAKE_VS_PLATFORM_NAME}_$<CONFIG>/
        ${CMAKE_CURRENT_SOURCE_DIR}/Source/Middleware/Box2D/lib/${CMAKE_VS_PLATFORM_NAME}_$<CONFIG>/
)

According to link, they do nothing on properties of static library project.

  • 1
    Does this answer your question? [CMake link to external library](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) Please see [this](https://stackoverflow.com/a/10550334/3987854) response for the modern CMake way to link pre-existing external libraries to your CMake targets. In particular, your syntax in `target_link_libraries` using the `;` semicolon is probably causing issues. – Kevin Aug 24 '20 at 20:14
  • 1
    Note that you don't need to build the list string yourself, you can simply pass multiple arguments to `target_link_libraries`. (e.g. `target_link_libraries(${PROJECT_NAME} PUBLIC SDL2 SDL2_image SDL2_mixer ...)`) – 0x5453 Aug 24 '20 at 20:15
  • According to [this](https://cmake.org/cmake/help/v3.18/command/target_link_directories.html) you should avoid using `target_link_directories` and instead pass full paths to `target_link_libraries`. Generally the way you would do this is by first calling `find_package` or `find_library` to find the full paths dynamically. – 0x5453 Aug 24 '20 at 20:17

0 Answers0