First you should read about the difference between and #include "filepath" here.#include <filepath>
Personally, I'm working with Boost from Visual Studio as follows:
boost library root (in my case C:\Program Files (x86)\Boost_1_53).#include <boost/lexical_cast/lexical_cast_old.hpp>If you're using non headers-only libraries you should also add path to Boost libraries in Project properties → Linker → General → Additional Libraries Directories.
you need the following commands:
export INCLUDE="/home/acerv3-571g/boost_1_58_0:$INCLUDE"
export LIBRARY_PATH="/home/acerv3-571g/boost_1_58_0/stage/lib:$LIBRARY_PATH"
you can execute them in a terminal to set these variables in the current terminal session. If you want to make them permanent, you have to add these lines to your .bashrc
CPM is just a thin layer around FetchContent, which in turn downloads your dependency into your build folder and then attempts to it, adding it to your main build.add_subdirectory
I think this is a bad idea for a lot of reasons...
I don't think any build using Boost should be any more complex than this:
cmake_minimum_required(VERSION 3.22)
project(boost-usage-example)
find_package(Boost 1.77 REQUIRED system date_time)
add_executable(nghttp2_asio main.cpp ...)
target_link_libraries(
nghttp2_asio
PRIVATE
Boost::boost # all header-only libs: config asio throw_exception assert align
Boost::date_time
Boost::system
)
See the documentation here: https://cmake.org/cmake/help/latest/module/FindBoost.html
You only list as components (after ) the non-header-only libraries. Those are also the ones that require special addition via REQUIRED. Link to target_link_libraries to get all the header-only modules.Boost::boost
This will work no matter what package manager you're using.
If you want to use , create a file called vcpkg in your project root with the following contents:vcpkg.json
{
"name": "boost-usage-example",
"version-string": "0.1.0-dev",
"dependencies": [
"boost-system",
"boost-config",
"boost-asio",
"boost-throw-exception",
"boost-assert",
"boost-align",
"boost-date-time"
]
}
You can also depend on just and it will acquire all boost modules, not just the ones you need. Then acquire vcpkg:boost
$ git clone https://github.com/microsoft/vcpkg.git
$ ./vcpkg/bootstrap-vcpkg.sh
Then build with:
$ cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=$PWD/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build build
Done!
Suppose on the other hand that you wanted to use Conan. No big deal, just install Conan in a pip virtual environment:
$ python3 -m venv venv
$ . venv/bin/activate
$ python3 -m pip install -U pip setuptools wheel
$ python3 -m pip install conan
Then create a with the following contents:conanfile.txt
[requires]
boost/1.77.0
[generators]
cmake_paths
Install the dependencies:
$ mkdir build && pushd build && conan install .. && popd
And then build, using Conan's generated toolchain file:
$ cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=$PWD/build/conan_paths.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build build
In fact, you can provide both a and a vcpkg.json and your users will be free to use either one or neither and rely on their system package manager or a package manager you don't know about. In any case it will just work and you free yourself of a mountain of maintenance burdens.conanfile.txt
The first variant,
Boost_INCLUDE_DIRS - Boost include directories
is among variables titled with
results are reported in variables:
These variable are for using (via read) in the after CMakeLists.txt.find_package(Boost)
The second variant,
Boost_INCLUDE_DIR - Directory containing Boost headers
is among variables titled with
saves search results persistently in CMake cache entries:
That is, you may set this CACHE variable (e.g. with option when call -D) for hint CMake about the Boost location.cmake
But preferable way for hint CMake about Boost location is to set variables in the list above, titled with
This module reads hints about search locations from variables:
When read these variables, CMake ( script) checks their value for correctness. E.g. it could be checked that directory stored in variable FindBoost.cmake actually contains some Boost headers.BOOST_INCLUDEDIR