Creating CMakeLists file from existing Makefile

Like this:

target_link_libraries(your-target-name pthread boost_thread-mt etc)

You should not use add_dependencies when you want to link libraries. Linking implies a dependency, but the dependency alone will not be sufficient when you need to link.


Unfortunately, there is no straightforward 1:1 conversion from Makefiles to CMakeLists. Since CMake is supposed to run on all platforms, it can not rely on platform specific assumptions like GNU make does, which complicates things in certain places.

In particular, CMake offers a very powerful and rather complex mechanism for using libraries: You call find_package with the name of your library, which will invoke a library search script from your cmake module path. This script (which is also written in CMake) will attempt to detect the location of the library's header and lib files and store them in a couple of CMake variables that can then be passed to the according CMake commands like include_directories and target_link_libraries.

There are two problems with this approach: First, you need a search script. Fortunately, CMake ships with search scripts for Pthreads, Boost and a couple of others, but if you are using a more exotic library, you might have to write the search script yourself, which is kind of an arcane experience at first...

The second major problem is that there is no standard way for a search script to return its results. While there are naming conventions for the used variables, those often don't apply. In practice that means you will have to check out a search script's source to know how to use it. Fortunately, the scripts that come with CMake are mostly very well documented.

The builtin scripts are located somewhere like <cmake-install-prefix>/share/cmake-2.8/Modules. For your question, look at the FindBoost.cmake and FindThreads.cmake files (CMake should automatically link with the standard library). Anycorn already gave some sample code for using the Boost script, everything else you need to know is in the CMake documentation or directly in the search script files.


With Boost you really need to use package finder

  set(Boost_ADDITIONAL_VERSIONS "1.46" "1.46.0" "1.46.1")
  set(Boost_USE_MULTITHREADED ON) # for -mt
  find_package(Boost COMPONENTS thread)
  if(Boost_FOUND)
    MESSAGE(STATUS "Found Boost: ${Boost_LIBRARY_DIRS}")
    MESSAGE(STATUS "Found Boost libraries: ${Boost_LIBRARIES}")
    set(LIBRARIES "${LIBRARIES};${Boost_LIBRARIES}")
  else()
    MESSAGE(FATAL_ERROR "Boost Thread NOT FOUND")
  endif()

target_link_libraries(executable ${LIBRARIES})