CMake - linking to library downloaded from ExternalProject_add()

前端 未结 6 1618
刺人心
刺人心 2020-11-29 19:34

I am trying to use ExternalProject_add() to download/install dependencies. It installs fine, but I can\'t figure out how to actually link the libraries after they are downlo

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 20:38

    You can use the link_directories command to link libraries within a specific directory. In your case the directory were your external project is build.

    ExternalProject_Add(MyExternalLibrary ...)
    

    Add the output directory to the search path:

    link_directories(${CMAKE_BINARY_DIR}/lib/MyExternalLibrary-prefix/lib)
    

    Make sure to add the executable after specifying the link directory:

    add_executable(MyProgram main.c)
    

    Specify the libraries your project should be linked to:

    target_link_libraries(MyProgram ExternalLibraryName)
    

    Don't forget to depend on the external project:

    add_dependencies(MyProgram MyExternalLibrary)
    

提交回复
热议问题