CMake: Link a library to library

后端 未结 2 1196
無奈伤痛
無奈伤痛 2021-02-04 00:34

I have a problem with cmake. I have, lets say, CMakeLists1 which has a subdirectory where CMakeLists2 is.

In CMakeLists2 my target is a static library. And I want to lin

相关标签:
2条回答
  • 2021-02-04 01:15

    I think it's CMake's default behavior to not link project2 to the external library, but to link both libraries to the executable. From the book "Mastering CMake".

    Since static libraries do not link to the libraries on which they depend, it is important for CMake to keep track of the libraries so they can be specified on the link line of the executable being created.

    You could try to use an absolute path in your CMakeLists2:

    add_library (project2 ${sources})
    target_link_libraries (project2 "path to ext lib"/"name of ext lib")
    

    or you could add

    link_directories ("path_to_library")
    

    to the CMakeLists file of project1.

    If you really want to do something like in Visual Studio, you could probably use the command given in this answer to build a custom_command in CMake. It probably would look something like this (I didn't test it).

    set(EXT_LIB "path_to_library/name_of_external_lib") 
    set(BIG_LIB "path_to_big_lib/name_of_big_lib")
    add_library (project2 ${sources})
    get_property(PROJ2_LOC TARGET project2 PROPERTY LOCATION)
    
    add_custom_command(OUTPUT ${BIG_LIB} 
                       DEPENDS ${EXT_LIB} project2
                       COMMAND "lib.exe /OUT:${BIG_LIB} ${EXT_LIB} ${PROJ2_LOC} )
    

    Then you could link your executable with ${BIG_LIB}.

    Some things you have to consider:

    • Maybe you have to use LOCATION_CONFIG (CMake docs, I found the get_property command in this answer )
    • link.exe has to be in your path
    • watch the scope of the BIG_LIB variable if you want to use it in an other CMakeLists.txt
    0 讨论(0)
  • 2021-02-04 01:35

    I'm guessing the trouble will likely be that *name_of_external_lib* is not correct so it can't find it.

    I would go with:

    find_library(
        LIB_I_NEED name_of_external_lib
        HINTS "path_to_library"
    )
    
    if(${LIB_I_NEED} STREQUAL "LIB_I_NEED-NOTFOUND")
        message(FATAL_ERROR "Couldn't find the 'external_lib' library)
    endif()
    
    message(STATUS "Found 'external_lib' at: ${LIB_I_NEED}")
    
    add_library (project2 ${sources})
    target_link_libraries (project2 ${LIB_I_NEED})
    

    If that doesn't help, have a quick read of the example in the cmake docs:

    http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:target_link_libraries

    One thing it mentions in there is:

    While one repetition is usually sufficient, pathological object file and symbol arrangements can require more. One may handle such cases by manually repeating the component in the last target_link_libraries call

    So I would say the other thing to try might be in project2:

    set(PROJECT_2_LIBS project2 "name_of_external_lib" PARENT_SCOPE)
    

    then in the exe:

    target_link_libraries (project1 ${PROJECT_2_LIBS})
    

    That will have the 'external_lib' linkage repeated in the two places and give you more chance of it working ;)

    0 讨论(0)
提交回复
热议问题