Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake link against dll/lib

The output of my cmake is a static library. I'm creating it as such:

add_library(myMainLib STATIC ${BACKEND_SOURCES})

Problems arise when I try to get myMainLib to link against a third party lib/dll. The dll file will be found at run time, however, I'm trying to import/link against the lib file, with no success. My third party library is SDL2 and SDL2 NET.

I would think this is straight forward and have exhausted all methods I've found online. All fail. A list of what I've tried is below. Please inform me what I'm doing wrong.

  1. Simple method, using target_link_libraries

    add_library(myMainLib STATIC ${BACKEND_SOURCES})
    
    target_link_libraries(myMainLib path_to_thirdPartyLib/thirdParty.lib)
    
  2. 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)
    
  3. Set path to library 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)
    
  4. Try finding the 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()
    
like image 280
h_b Avatar asked Aug 19 '14 20:08

h_b


1 Answers

In CMake and several build systems directly linking a static library into another static library is meaningless. You can build a static library and a second one and have your executable project linked against both, but it's not possible to link the first static library with the second library and then link them into the final executable. Although VS allows that, it doesn't make sense for other build systems and thus CMake refrains from it.

Some solutions involve making your static library a shared one or pull the library sources into the executable.

Other details here

like image 195
Marco A. Avatar answered Oct 20 '22 04:10

Marco A.