CMake: Link a library to library

后端 未结 2 1200
無奈伤痛
無奈伤痛 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: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 ;)

提交回复
热议问题