CMake link to external library

后端 未结 4 2104
梦谈多话
梦谈多话 2020-11-21 04:53

How to get CMake to link an executable to an external shared library that is not build within the same CMake project?

Just doing target_link_libraries(GLBall $

4条回答
  •  别那么骄傲
    2020-11-21 05:23

    arrowdodger's answer is correct and preferred on many occasions. I would simply like to add an alternative to his answer:

    You could add an "imported" library target, instead of a link-directory. Something like:

    # Your-external "mylib", add GLOBAL if the imported library is located in directories above the current.
    add_library( mylib SHARED IMPORTED )
    # You can define two import-locations: one for debug and one for release.
    set_target_properties( mylib PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/res/mylib.so )
    

    And then link as if this library was built by your project:

    TARGET_LINK_LIBRARIES(GLBall mylib)
    

    Such an approach would give you a little more flexibility: Take a look at the add_library( ) command and the many target-properties related to imported libraries.

    I do not know if this will solve your problem with "updated versions of libs".

提交回复
热议问题