Link library boost with cmake?

2

I'm trying to use c ++ boost library. Reading a tutorial on their website, I came across this and do not know how to do it. It is said that I have to include #include <boost/test/unit_test.hpp> and make a link with this lib: libunit_test_framework.lib. I do not want to use the pre-copilada, which is the option they give them down, or they can do it the first way. How do I make this link with this lib?

Source: Boost Test Library Tutorial

    
asked by anonymous 19.11.2017 / 18:36

1 answer

0

You can use the FindBoost module, which comes in the standard CMake installation, to include boost in your project. The syntax is as follows:

find_package(Boost
  [version] [EXACT]      # Versão mínima ou exata (EXACT).
  [REQUIRED]             # Emita um erro se Boost não for encontrado.
  [COMPONENTS <libs>...] # Use bibliotecas específicas do Boost.
  )

For example, to use Boost in the exact 1.65.1 version, the required package for the build to be successful, with the Filesystem , Regex < in> and Algorithm :

find_package(Boost 1.65.1 EXACT REQUIRED COMPONENTS filesystem regex)

If the boost package is found and its version hits the specified one, then the library will be available for use in the rest of your CMakeLists.txt, through the Boost:: namespace.

The thoughtful reader will notice that there is no mention of the Algorithm library above. Since this library has the header-only feature (implemented all in one header), there is no component to be required as there is nothing to build and link. Including the header is enough. You can link the target Boost::boost to the target of your application and include directories automatically.

For example, the following CMakeLists.txt :

cmake_minimum_required(VERSION 3.9)
project(ProjetoExemplo)
include(FindBoost)
find_package(Boost 1.65.1 EXACT REQUIRED COMPONENTS filesystem regex)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE Boost::boost Boost::filesystem Boost::regex)

Compiling the simple example below as main.cpp :

#include "boost/algorithm/cxx11/none_of.hpp"
#include "boost/filesystem.hpp"
#include "boost/regex.hpp"

int main() {}

Running the following commands:

cmake .
cmake --build .

It should generate an executable called app . Note that the inclusions of the boost headers are made using "..." and not <...> . That's because the include directories are passed to the compiler by CMake, so it's best to use them that way.

    
17.12.2017 / 21:55