How do I tell CMake to link in a static library in the source directory?

前端 未结 3 385
囚心锁ツ
囚心锁ツ 2020-11-27 11:51

I have a small project with a Makefile which I\'m trying to convert to CMake, mostly just to get experience with CMake. For purposes of this example, the project contains a

相关标签:
3条回答
  • 2020-11-27 12:18

    I found this helpful...

    http://www.cmake.org/pipermail/cmake/2011-June/045222.html

    From their example:

    ADD_LIBRARY(boost_unit_test_framework STATIC IMPORTED)
    SET_TARGET_PROPERTIES(boost_unit_test_framework PROPERTIES IMPORTED_LOCATION /usr/lib/libboost_unit_test_framework.a)
    TARGET_LINK_LIBRARIES(mytarget A boost_unit_test_framework C)
    
    0 讨论(0)
  • 2020-11-27 12:34

    CMake favours passing the full path to link libraries, so assuming libbingitup.a is in ${CMAKE_SOURCE_DIR}, doing the following should succeed:

    add_executable(main main.cpp)
    target_link_libraries(main ${CMAKE_SOURCE_DIR}/libbingitup.a)
    
    0 讨论(0)
  • 2020-11-27 12:38

    If you don't want to include the full path, you can do

    add_executable(main main.cpp)
    target_link_libraries(main bingitup)
    

    bingitup is the same name you'd give a target if you create the static library in a CMake project:

    add_library(bingitup STATIC bingitup.cpp)
    

    CMake automatically adds the lib to the front and the .a at the end on Linux, and .lib at the end on Windows.

    If the library is external, you might want to add the path to the library using

    link_directories(/path/to/libraries/)
    
    0 讨论(0)
提交回复
热议问题