cmake link against dll/lib

我怕爱的太早我们不能终老 提交于 2021-02-07 12:26:18

问题


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()
    

回答1:


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



来源:https://stackoverflow.com/questions/25392174/cmake-link-against-dll-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!