Cmake link to dll / lib

The output of my cmake is a static library. I create it as such:

add_library(myMainLib STATIC ${BACKEND_SOURCES}) 

Problems arise when I try to connect myMainLib to a third-party lib / dll. The dll file will be found at runtime, however I am trying to import / link to the lib file, without success. My third-party library is SDL2 and SDL2 NET.

I would think that it was straightforward and exhausted all the methods that I found on the Internet. Everyone fails. Below is a list of what I have tried. Please tell me what I am doing wrong.

  • Simple method using target_link_libraries

     add_library(myMainLib STATIC ${BACKEND_SOURCES}) target_link_libraries(myMainLib path_to_thirdPartyLib/thirdParty.lib) 
  • According to cmake docs

     add_library(myMainLib STATIC ${BACKEND_SOURCES}) add_library(Third_Party SHARED IMPORTED) set_property(TARGET Third_Party PROPERTY IMPORTED_LOCATION path_to_thirdPartyLib/thirdParty.dll) set_property(TARGET Third_Party PROPERTY IMPORTED_IMPLIB path_to_thirdPartyLib/thirdParty.lib) target_link_libraries(myMainLib Third_Party) 
  • Set library path using link directories

     add_library(myMainLib STATIC ${BACKEND_SOURCES}) set(LIB_DIR path_to_thirdPartyLib) LINK_DIRECTORIES(${LIB_DIR}) target_link_libraries(myMainLib ${LIB_DIR}/thirdParty.lib) 
  • Try to find a library

     add_library(myMainLib STATIC ${BACKEND_SOURCES}) find_library(Third_Party thirdParty.lib) if(Third_Party) #never gets in here target_link_libraries(myMainLib ${Third_Party}) endif() 
+7
c ++ dll cmake static-libraries
source share
1 answer

In CMake and several build systems that directly link the static library to another static library, it makes no sense. You can create a static library and a second one, and your executable project is linked to both, but it is impossible to link the first static library to the second library, and then link them to the final executable. Although VS allows this, it does not make sense for other build systems and, therefore, CMake refrains from it.

Some solutions include creating your static shared library or pulling library sources into an executable.

Other details here

+2
source share

All Articles