Add library to Cmake project

11.4k views Asked by At

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

There are 2 answers

0
SingerOfTheFall On BEST ANSWER

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.

0
Dmitry On

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(YOUR_TARGET_NAME async_waiter)