Add library to Cmake project

2019-02-28 06:56发布

Perhaps I simply can't find it, but I want to add some code to a project of mine (libunwind found here http://www.nongnu.org/libunwind/download.html)
This library does not come with a CMakeLists.txt file and when I try to include it cmake complains about this fact. Right now I've simply added the libunwind directory to my external code and added a reference in my main CMakeLists.txt

Any input would be great.

2条回答
对你真心纯属浪费
2楼-- · 2019-02-28 07:11

If you want to build it each time along with your project, the easiest way would be to:

  • Add the source code somewhere into your project tree
  • Add a custom CMake target which should run before the compilation starts
  • In that custom target, run whatever is needed to compile the library (in your case it's ./configure -> make -> make install.

However that is rarely needed and most of the times you should just build the library once and link it as any other external library.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-28 07:11

Dealing with libraries there are 2 options for you :

  1. If you've downloaded and was able to build and install it you can try to find it later on inside you CMAKE like this ( in case of Boost ) and link to your target:

    find_package( Boost COMPONENTS date_time system serialization thread program_options filesystem unit_test_framework regex chrono REQUIRED )

    if( NOT Boost_FOUND ) message( FATAL_ERROR "Cannot find boost!" ) endif( NOT Boost_FOUND )

    message(STATUS "boost found")

    include_directories( ${Boost_INCLUDE_DIRS} ) link_directories( ${Boost_LIBRARY_DIRS} )

    target_link_libraries(YOUR_TARGET_NAME ${Boost_LIBRARIES})

  2. You can add external library sources as a stand-alone target and use smth like this for CMake to build it :

    set (sources async_waiter.h async_waiter_impl.h async_waiter_impl.cpp )

    add_library( async_waiter ${sources} )

and later on link you target to it with :

target_link_libraries(YOU_TARGET_NAME async_waiter)
查看更多
登录 后发表回答