1
votes

I use cmake to build on Windows, Linux, and OSX. On Windows, I use .dll and .lib files that I have prebuilt and put it in a folder project/windows/bin, project/windows/include, and project/windows/lib. These folders house all my third party dependencies for windows. In my CMakeLists.txt, I use:

if(WIN32)
  set(CMAKE_PREFIX_PATH ${PROJECT_SOURCE_DIR}/windows)
endif()

find_package(SDL2 REQUIRED)
find_package(GLEW REQUIRED)

It works but I am only able to use one configuration of the library. I would like to be able to link different configuration of the library like Debug and Release.

The question is : How do I make it so that when I set my visual studio project to debug, it will use the debug version of the library and use the release version of the libray when I set the visual studio project to release?

Do I need to set up my /windows library directory differently?

Thanks in advance

1
Does this SO question/answer address your question?aichao

1 Answers

2
votes

I'm now sure, whether the FindSDL2 and FindGLEW modules you use provide imported targets. If so, then might pick up the respective library as both debug and release and you should use the imported target for linking.

Otherwise, you have two options:

  1. Explicitly use optimized <LIB1_release> debug <LIB1_debug> as referenced by @aichao in the other SO question/answer:

    target_link_libraries(MyConsumerTarget
                          PUBLIC optimized <LIB1_release>
                                 debug <LIB1_debug>)
    
  2. Manually create imported library targets for each external library and use them for linking:

    if(NOT TARGET External::lib1) # this if is required for subsequent runs of CMake
      add_library(External::lib1 SHARED IMPORTED GLOBAL)
      set_target_properties(External::lib1
                            PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/windows/include"
                                       IMPORTED_LINK_INTERFACE_LANGUAGES "C"
                                       IMPORTED_LOCATION_RELEASE "${PROJECT_SOURCE_DIR}/windows/bin/<LIB1_release>.dll"
                                       IMPORTED_IMPLIB_RELEASE "${PROJECT_SOURCE_DIR}/windows/lib/<LIB1_release_importlib>.lib"
                                       IMPORTED_LOCATION_DEBUG "${PROJECT_SOURCE_DIR}/windows/bin/<LIB1_debug>.dll"
                                       IMPORTED_IMPLIB_DEBUG "${PROJECT_SOURCE_DIR}/windows/lib/<LIB1_debug_importlib>.lib")
     endif()
    
     target_link_libraries(MyConsumerTarget
                           Public External::lib1)
    

Personally, I prefer the latter as it is less verbose in the main CMakeLists file. The definition of the different libraries can be done in other files included via other means.
Usually, I have a directory 3rdparty in my projects with a CMakeLists.txt file, which pulls in external projects and defines such imported targets. In the main CMake file I can then easily use these imported targets.