Setup Boost in Clion - c++

How to use Boost library in Clion with MinGW ? I have downloaded and unzipped boost_1_60_0.zip to C:\boost_1_60_0. What am I supposed to do now ? Do I have to install something ? Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(server_client)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -s -O3")
set(CMAKE_EXE_LINKER_FLAGS -static)
set(BOOST_ROOT "C:/boost_1_60_0")
set(BOOSTROOT "C:/boost_1_60_0")
find_package(Boost 1.60.0)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
set(SOURCE_FILES chat_server.cpp)
add_executable(server_client ${SOURCE_FILES})
Can not find Boost:

I use MinGW distro by Stephan T. Lavavej with Boost libraries prebuilt.
In my cmaklist.txt I added this
set(Boost_INCLUDE_DIR c:/mingw/include/)
set(Boost_LIBRARY_DIR c:/mingw/lib/)
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
This post help me get it going. How to include external library (boost) into CLion C++ project with CMake?

Here's example project for CLion that uses Boost's regex library. It references to this tutorial.
Goal: with CLion create .exe that process jayne.txt as shown in Boost's tutorial.
My example revolves around building Boost Library Binary with GCC, configuring project in CLion and using CMake. It's quite expressive, maybe even an overkill, but I believe you can adapt. On the other hand I'll try to make project itself as system independent as its possible.
Configuration:
OS: Windows 8.1
CLion: 2018.2.4
Boost: 1.68.0
compiler: GCC-6.3.0 (provided by MinGW)
MinGW was configured according to its instructions and JetBrains's suggestions. (I downloaded setup for MinGW 14/10/2018 if that matters.)
A word about tools structure
I decided to create following structure for Boost and things aroung it:
D:\
SDK\
boost\
boost_1_68_0\ # untouched Boost root
boost\
rst.css
...other directories and files...
1_68_0\
build\ # dir for Boost.Build, b2.exe
buildDir\ # intermediate dir for creating libraries
stageDir\ # dir for libraries
lib\
MinGW\
bin\
...other directories...
I left Boost root untouched -- I didn't create any additional directories. This separates Boost sources from created tools and libraries so I can show how to specify these directories explicitly.
Obtain Boost Library Binary
I decided to build libraries from source with GCC.
Download and unpack Boost ("D:\SDK\boost\boost_1_68_0");
Build libraries (5.2):
Open command prompt at \tools\build of Boost root ("D:\SDK\boost\boost_1_68_0\tools\build")
Run bootstrap.bat
Run b2 install --prefix="D:\SDK\boost\1_68_0\build" --toolset=gcc-6.3.0. This creates b2.exe under "D:\SDK\boost\1_68_0\build\bin"
Move command prompt to Boost root (cd "D:\SDK\boost\boost_1_68_0")
(You can consider using "D:\SDK\boost\1_68_0\build\bin\b2.exe --show-directories". It's worth to specify libraries to build (--with-library-name) in the following command, because this step can take a while.) Run "D:\SDK\boost\1_68_0\build\bin\b2" --build-dir="D:\SDK\boost\1_68_0\buildDir" toolset=gcc-6.3.0 --build-type=complete stage --stagedir="D:\SDK\boost\1_68_0\stageDir" --with-regex. It creates following files under D:\SDK\boost\1_68_0\stageDir\lib directory:
libboost_regex-mgw63-mt-d-x32-1_68.a
libboost_regex-mgw63-mt-d-x32-1_68.dll
libboost_regex-mgw63-mt-d-x32-1_68.dll.a
libboost_regex-mgw63-mt-sd-x32-1_68.a
libboost_regex-mgw63-mt-s-x32-1_68.a
libboost_regex-mgw63-mt-x32-1_68.a
libboost_regex-mgw63-mt-x32-1_68.dll
libboost_regex-mgw63-mt-x32-1_68.dll.a
CMake looks for files with specific names in library folder, so be sure to (copy and) change name for one of .a files there. For this example, I changed libboost_regex-mgw63-mt-x32-1_68.a to boost_regex.a.
Create CLion project
Create a new project (for this example its name is CLionBoostRegex) and put content to main.cpp (6):
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
Configure CLion
Go to (on Windows) File -> Settings -> Build, Execution, Deployment -> CMake, and in CMake options add path to Boost root directory with -DBOOST_ROOT=, i.e.: -DBOOST_ROOT="D:\SDK\boost\boost_1_68_0". If directory with built libraries is placed outside Boost root, add it with -DBOOST_LIBRARYDIR=, i.e.: -DBOOST_LIBRARYDIR="D:\SDK\boost\1_68_0\stageDir\lib". Commands are space separated.
Decide if you want static or dynamic linking.
Option 1: Static linking
For static linking your CMakeLists.txt should look like this:
cmake_minimum_required(VERSION 3.12)
project(CLionBoostRegex)
set(CMAKE_CXX_STANDARD 98)
find_package(Boost REQUIRED COMPONENTS regex)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(CLionBoostRegex main.cpp)
target_link_libraries(CLionBoostRegex -static)
target_link_libraries(CLionBoostRegex ${Boost_LIBRARIES})
Option 2: Dynamic linking
CMakeLists.txt should look like for static linking, but remove target_link_libraries(CLionBoostRegex -static) line.
After building your project make sure to copy .dll library to directory with executable (libboost_regex-mgw63-mt-x32-1_68.dll) along with libstdc++-6.dll from MinGW\bin directory (D:\SDK\MinGW\bin) (or consider including target_link_libraries(CLionBoostRegex -static-libstdc++) line in CMakeLists.txt or add -DCMAKE_CXX_FLAGS="-static-libstdc++" to CMake options in settings).
Run your program (6.4)
Build your target (it creates cmake-build-debug\ directory with default config) (if you picked dynamic linking make sure to add necessary .dlls)
In directory with your executable create jayne.txt file with following content:
To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
Open command prompt there
Run CLionBoostRegex.exe < jayne.txt
Program should output Will Success Spoil Rock Hunter? as shown in tutorial.
Notes
Changing name for an .a library and choosing -static linking causes the least effort afterwards - you won't have to copy any additional libraries at the price of bigger executable size. When executable size is more important, you can change name for .dll library in Boost libraries directory instead and then copy missing .dlls for your .exe (i.e.: libboost_regex-mgw63-mt-x32-1_68.dll and libstdc++-6.dll).
You can include set(Boost_DEBUG ON) line in your CMakeLists.txt or -DBoost_DEBUG=1 to CMake options to get some precious info.
I used other questions to write this post, most notably: 1, 2, 3, 4.

Related

Undefined reference errors when trying to compile and simple ImageMagick program

I've been searching for a solution to this problem for a long time to no avail.
I am trying to compile this simple program:
#include <iostream>
#include <Magick++.h>
#include <Magick++/Image.h>
using namespace std;
using namespace Magick;
int main(int argc,char **argv) {
InitializeMagick("D:\\Programming\\CPPProjects\\NoteScripts\\Dependencies\\magick\\include");
Image image;
// image.read("arch");
// image.write("test.png");
}
Upon building, I get the following error:
CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1c): undefined reference to `Magick::InitializeMagick(char const*)'
CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x28): undefined reference to `Magick::Image::Image()'
CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x34): undefined reference to `Magick::Image::~Image()'
collect2.exe: error: ld returned 1 exit status
From what I can tell, this is a linker error but I have no idea where I am going wrong with linking the libs needed.
I installed ImageMagick on Windows 10 from the ImageMagick downloads page with this installer: ImageMagick-7.1.0-50-Q16-HDRI-x64-dll.exe
I then copied the lib files from the lib folder under the installation directory into my project and then copied the include folder under the installtion directory into my project.
Here is what the project hierarchy looks like (Source Directory is NoteScripts):
My CMakeLists.txt consists of:
cmake_minimum_required(VERSION 3.10)
set( CMAKE_CXX_COMPILER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe")
set( CMAKE_C_COMPILER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe" )
# set the project name
project("Notes")
include_directories(D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/include)
# add the executable
add_executable(main main.cpp)
target_link_libraries(main D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/lib/CORE_RL_Magick++_.lib)
target_link_libraries(main D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/lib/CORE_RL_MagickCore_.lib)
target_link_libraries(main D:/Programming/CPPProjects/NoteScripts/Dependencies/magick/lib/CORE_RL_MagickWand_.lib)
If I comment out lines 9 and 10 where InitializeMagick() is called and where Image image is declared, the program compiles without error. I'm also aware that the order of the static libs listed out matters but trying out multiple combinations has resulted in the same error. I have also verfied the dependency order by sifting through the original source code and the reference path is Magick++ -> MagickCore -> MagickWand.
I am relatively new to the process of adding external dependencies to my C++ projects so this is unfamiliar territory (coming from languages with clean package managers). Any help as to how to fix this issue is greatly appreciated!
The typical (and easiest) way of handling dependencies in CMake is using its find_package command:
find_package(ImageMagick REQUIRED COMPONENTS MagickCore MagickWand Magick++)
// ...
target_link_libraries(main ${ImageMagick_LIBRARIES})
This method is available for ImageMagick with your CMake version. I'm not familiar with CMake on Windows, but find_package by default searches a number of standard (system) locations for the package's files. Since you have a custom setup, it should also be possible to specify a nonstandard search prefix to the command. Additionally, you could download external dependencies in a portable way with the FetchContent commands.
First of all, it is a pain to setup this thing if you are a newbie like me.
Now to the steps to dynamically link imagemagick libs with your C app:
go to https://github.com/ImageMagick/ImageMagick-Windows and follow the instructions there (Install Visual Studio dependencies - Clone the dependencies - Build configure.exe- Build ImageMagick)
in the step Build configure.exe, when running configure.exe, keep the default option selected when asked about the output library type (keep it set to dynamic)
in the Build ImageMagick step, when you open VisualDynamicMT.sln in visual studio, before you start the build, select all the solutions in the project, and right-click -> properties -> General -> C Language Standard -> choose Default (Legacy MSVC). After that, click on the top-most solution that contains all the other 196 solutions, and build it. watch the console for errors, I didn't get any errors with the configuration above.
After the latter step, go the VisualMagick folder (created from steps before), and you will see lib folder and bin folder. You're done, your dlls are in bin and your .lib file are in bin folder. Note that these files will be corresponding to build or release environments depending on what you selected in visual studio at the build step.
How do you use imagemagick now in your project regardless if you have imagemagick app installed on your pc or not? Lets create a new project in vscode, call it demo.
Create this project structure:
inside src you will put your C code.
inside deps create ImageMagick/lib and ImageMagick/bin and ImageMagick/include
inside ImageMagick/include place the same include files you said you got in your question.
inside ImageMagick/lib place the .lib files you got from above
inside ImageMagick/bin place the .dll files you got from above
now add this to your CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(demo-app C)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
file(GLOB SOURCE_FILES src/*.c)
add_executable(demo ${SOURCE_FILES})
include_directories(src)
# ImageMagick
if(WIN32)
add_definitions( -DMAGICKCORE_QUANTUM_DEPTH=16 )
add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
include_directories(deps/ImageMagick/include)
target_link_directories(demo PRIVATE deps/ImageMagick/lib)
file(GLOB IMAGEMAGICK_LIBS deps/ImageMagick/lib/*.lib)
target_link_libraries(demo
"${IMAGEMAGICK_LIBS}"
)
add_custom_command(TARGET demo POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/deps/ImageMagick/bin"
$<TARGET_FILE_DIR:demo>)
endif()
(The add_custom_command will copy the dlls to your executables path after every build)
Now write some magick code in you src directory.
ctrl + shift + A -> CMake select kit -> choose the Visual Studio community 2022 release -amd64 or change to what fits you if it doesn't work
ctrl + shift + A -> CMake Clean Rebuild
ctrl + shift + A -> CMake run without debugging

CMake project in Visual Studio: How to add additional include and library directories?

I am developing C++ code using VisualStudio 2019.
I am using CMake to configure the project.
I need to use boost library which is compiled on my remote machine.
In console application, I can put the path of the include files I need when I go to the Properties of the project under Additional Include Directories field.
And under Additional Include Directories I can put the path of boost library.
Now I can not find Properties when I right-click on my project to add what I need.
My boost include directory is under /home/ubuntu/boost_1_70_0
My boost libraries directory is under /home/ubuntu/boost_1_70_0/stage
How can add them in my CMake project?
Thank you!
EDIT:
This is my CMakelists.txt file:
# CMakeList.txt : CMake project for CMakeProject1, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED OFF)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.70.0 REQUIRED COMPONENTS lambda)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(CMakeProject1 CMakeProject1.cpp)
target_link_libraries(CMakeProject1 ${Boost_LIBRARIES})
endif()
# TODO: Add tests and install targets if needed.
And this is my .cpp file:
#include "CMakeProject1.h"
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
using namespace std;
int main()
{
typedef std::istream_iterator<int> in;
std::cout << "Type in any number: ";
std::for_each(
in(std::cin), in(), std::cout
<< (boost::lambda::_1 * 10)
<< "\nType in another number: ");
}
The path of my boost directory is: /home/ubuntu/boost_1_70_0
The path of my boost libraries is: /home/ubuntu/boost_1_70_0/stage
When I run the .cpp file this CMake error occurs:
Error CMake Error at CMakeProject1/CMakeLists.txt:13 (find_package):
Could not find a package configuration file provided by "Boost"
(requested version 1.70.0) with any of the following names:
BoostConfig.cmake
boost-config.cmake
Add the installation prefix of "Boost" to CMAKE_PREFIX_PATH or set
"Boost_DIR" to a directory containing one of the above files. If
"Boost" provides a separate development package or SDK, be sure it
has been installed.
CMake's find_package command has two modes: Module and Config mode. Many related questions on this site feature answers that are using Module mode. However, Boost 1.70 and greater provides a BoostConfig.cmake or boost-config.cmake package configuration file to easily use with find_package() Config mode. The package configuration file should be generated when you build Boost. For example, if you build Boost 1.72 into the stage directory, the BoostConfig.cmake file is located here:
boost_1_72_0/stage/lib/cmake/Boost-1.72.0/BoostConfig.cmake
Your error indicates that you are using Config mode, so you have two options:
Option 1
Do the steps recommended in the error message to help the Config mode package search complete successfully. Append the path to your Boost installation to the prefix path by adding this line to your CMake file before find_package():
list(APPEND CMAKE_PREFIX_PATH /home/ubuntu/boost_1_70_0)
Option 2
Force Module mode by setting the Boost_NO_BOOST_CMAKE variable before the call to find_package():
set(Boost_NO_BOOST_CMAKE ON)

Adding Boost to CMake project?

Background
I'm a complete newb with C++ and I've been running into one headache after another, so forgive me if this is incredibly simple and I'm just that dumb...
I have a project that should ultimately compile and run in Linux. Unfortunately after lots of issues with my C++ development environemnt (still unresolved), I gave up on trying to develop in Linux and moved to Windows Visual Studio 2017. My hope was to get my code working in Windows and then, since C++ is supposedly a portable language, it should just work in Linux with minimal changes.
For a day or so Visual Studio seemed to be working. I could write code, hit "compile", and like magic it would run. I threw together a few classes to construct a directed acyclic graph, used the standard library for a hash table, and then I tried to create a socket...
Windows and Linux use different libraries for sockets (<sys/socket.h> vs <winsock.h>) so I needed some way to abstract the differences, and I preferred a well-established standard. Googling around I found the Boost library that seemed to fit my needs... That's when everything went to hell.
My project setup
Because this project will be developed across a variety of platforms and IDEs (some people use Windows + Visual Studio, some people use Mac + Eclipse, and others use Linux + VIM) I opted to make it a CMake project. After several hours of reading and learning and research it seems like CMake should give me what I want (convenient and reproducible cross-platform builds with little or no dependency issues)
My source code (directly from the Boost Getting Started on Windows guide) is as follows:
CMakeProject2.cpp
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " ");
}
Per the Boost Getting Started on Windows guide, I downloaded Boost from here:
https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.zip
(interestingly, the Getting Started guide is titled "Boost Getting Started on Windows - 1.69.0", yet it linked to Boost versions 1.67.0)
After downloading and extracting the ZIP file, I had a whole mess of files - but no idea where to put them:
Attempts to Get It Working So Far
I tried to add the Boost library to my project, but none of the expected menu options were available:
Although I couldn't find a single page that warns you of this gotcha, apparently CMake projects don't have the elusive "Properties" window - and instead third party libraries must somehow be included via the CMakeLists.txt file
For starters, I copied the entire 540 MB contents of the Boost ZIP file to within my project under the folder name "Boost":
I then tried a series of different CMakeList.txt commands:
Per How do you add Boost libraries in CMakeLists.txt?:
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS lambda)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h")
target_link_libraries(CMakeProject2 ${Boost_LIBRARIES})
endif()
Per https://www.selectiveintellect.net/blog/2016/7/29/using-cmake-to-add-third-party-libraries-to-your-project-1:
include("Boost")
add_subdirectory("Boost")
add_subdirectory("boost")
add_subdirectory("Boost/boost")
add_subdirectory("Boost/boost/lambda")
target_link_libraries(boost)
target_link_libraries(Boost)
Per https://cmake.org/pipermail/cmake/2009-November/033249.html:
SET (Boost_FIND_REQUIRED TRUE)
SET (Boost_FIND_QUIETLY TRUE)
SET (Boost_DEBUG FALSE)
set (Boost_USE_MULTITHREADED TRUE)
set (Boost_USE_STATIC_LIBS TRUE)
SET (Boost_ADDITIONAL_VERSIONS "1.67" "1.67.0")
FIND_PACKAGE(Boost COMPONENTS lambda)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
I tried several other incantations (not being familiar with C++ or CMake as a tool) and either received errors from CMakeLists.txt, or from CMakeProject2.cpp about cannot open source file "boost/lambda/lambda.hpp". In fact, with regards to those "CMakeLists.txt" errors, after adding enough lines to my file I started to crash Visual Studio regularly. Note that I have an 8th generation i7, 32 gigabytes of RAM, and an M.2 NVMe hard drive -- so I was rather impressed that a few lines in a text file pissed off Microsoft enough to lock up my computer for 10 minutes at a time.
Failing all of that, I tried copying the files I needed directly into my project:
Now, again, I'm new to C/C++ development and everything that can go wrong has gone wrong. So far I've spent almost two weeks and barely managed to compile a single "Hello, World" app across two computers, three IDEs, and four compilers. I've yet to have any success including a third party library, from anywhere, of any popularity level or simplicity level, and actually compile a functioning program that references the library. So believe me when I say: I don't know the difference between a "header-only library" and... whatever the alternative is. I just know that, according to the Boost Getting Started on Windows guide, most of Boost is "headers only" and therefore I shouldn't have any build step -- it should be simple to use it. Furthermore, this example (using boost::lambda) is - per their instructions - a header-only library, and should therefore be extremely easy to use.
I now updated the source code slightly to look in the current directory, instead of looking in the system include directory (which, as far as I'm aware at this point, doesn't exist in Windows):
#include "boost/lambda/lambda.hpp"
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " ");
}
Now I can manually verify that this file exists (the file CMakeProject2\CMakeProject2\boost\lambda\lambda.hpp can be found in File Explorer) - yet I'm still getting errors:
cannot open source file "boost/lambda/lambda.hpp"
Some further Googling led me to update my CMakeLists.txt file once more, putting it in its current form:
# CMakeList.txt : CMake project for CMakeProject2, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
file(GLOB CMakeProject2_SRC
"*.h"
"*.cpp"
"**/*.h"
"**/*.cpp"
"**/*.hpp"
"boost/lambda/lambda.hpp"
)
add_executable (CMakeProject2 ${CMakeProject2_SRC})
#add_executable (CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h")
# TODO: Add tests and install targets if needed.
Despite this I'm still getting the error:
cannot open source file "boost/lambda/lambda.hpp"
At this point I'm ripping my hair out. What am I doing wrong? What do I not know? How is something as simple as the Boost-equivalent of "Hello, World" not working for me?
Following recipe should work
Download Boost binaries from official boost binaries location and install to say C:\Boost
Most times you do not need to build Boost on your own.
Your CMakeLists.txt should look like follows
cmake_minimum_required (VERSION 3.8)
project(boostAndCMake)
set(BOOST_ROOT "C:\Boost") # either set it here or from the command line
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system) # header only libraries must not be added here
add_executable(CMakeProject2 CMakeProject2.cpp CMakeProject2.h)
target_include_directories(CMakeProject2 PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(CMakeProject2 ${Boost_LIBRARIES})
Because we used REQUIRED on the find_package call, CMake will fail execution and skip the rest of the script if it cannot be found. So no need to check Boost_FOUND. You need to check it, when you omit REQUIRED.
Now from the command line call from the directory where your script resides:
cmake -H. -Bbuildit -G "Visual Studio 15 2017" -DBOOST_ROOT=C:\Boost
This creates a build directory named buildit in the current directory, further creates a solution for Visual Studio 2017 inside the build directory and provides the setting for the variable BOOST_ROOT that is used in the find_package call to identify the Boost directory on your computer. To see what options are available on the find_package(Boost ...) call see FindBoost documentation in CMake.
Header Only Libraries
If your libraries are header only you need to omit them from the find_package(Boost ...) call. To see which libraries are not header only see this post.
Using newer Boost versions
If your CMake installation cannot find the requested version, e.g. 1.69.0, but supports the naming scheme of the more recent Boost version you can use it with set(Boost_ADDITIONAL_VERSIONS "1.69.0" "1.69"). Last change of the Boost naming scheme was from 1.65.1 to 1.66.
Here's a working setup for Boost 1.68 with CMake 3.12. Boost 1.69 is apparently "too new" for cmake to detect it properly. Since boost is not buildable by cmake, cmake itself must provide a FindBoost.cmake module that must keep up with boost changes.
So anyway, the CMakeLists.txt is as small as this:
cmake_minimum_required(VERSION 3.11)
project(foobar)
find_package(Boost 1.68 REQUIRED)
add_executable(foo foo.cpp)
target_link_libraries(foo PUBLIC Boost::boost)
Of course, you can split it in many subdirectories.
Invoking CMake in the command line should look like this:
cmake -DCMAKE_PREFIX_PATH=path_to_local_directory ..
Where path_to_local_directory is the installation path of all library you want to depend on. It will work for Boost, nlohmann_json, glfw3, Qt, you name it *(1). For my case, it was C:/local/ and another case was ../external/ (yes, it can be a directory local to the project!)
Let's take a peek at my own C:/local/:
ls -l /c/local/
total 12
drwxr-xr-x 1 myself 197609 0 May 26 2018 boost_1_67_0/
drwxr-xr-x 1 myself 197609 0 Sep 5 02:02 boost_1_68_0/
WARNING: Ensure your compiler architecture is the same as the installed boost version. Or else cmake will simply not find it.
I think that about it. The next CMake version (3.14) should work with the latest boost.
*(1) The said library will either need to export it's CMake target or you must provide a FindXXX.cmake
I'm using CMake 3.22 with Boost version 1.78.
The simplest solution is to set the Boost_INCLUDE_DIR when calling Cmake:
cmake -DBoost_INCLUDE_DIR=boost
Pass the directory to where the Boost libraries are. If you're using Visual Studio you can also set this in your CMake Settings:
Or, in the CMakeSettings.json file:
"cmakeCommandArgs": "-DBoost_INCLUDE_DIR=boost",
In my opinion, this is better than using the set function because you're not hard coding the path.
Add a target_include_directories(CMakeProject2 PRIVATE .) into your CMakeList.txt.
The . is the relative path of boost/lambda/lambda.hpp from CMakeLists.txt
And you should not add any .hpp file to the source list.

Build a boost project using cmake in msvc

I have written a code to read a compressed file directly. I have to build it using Cmake. This is my only source file, uncompress.cpp
#include <iostream>
#include <vector>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
// cl /EHsc uncompress.cpp
std::vector<char> & readline(std::istream & stream, std::vector<char> & container) {
char c;
container.clear();
while (stream && stream.get(c)) {
container.push_back(c);
if (c == '\n') break;
}
return container;
}
int main () {
boost::iostreams::filtering_istream in;
boost::iostreams::filtering_istream cinn(std::cin);
in.push(boost::iostreams::bzip2_decompressor());
in.push(cinn);
std::vector<char> line;
while (readline(in, line).size() != 0) {
std::string str(line.begin(), line.end());
std::cout << "--" << str ;
}
}
My CMakeLists.txt includes
cmake_minimum_required (VERSION 2.8.9)
project (reader-demo CXX C)
find_package (Boost COMPONENTS iostreams zlib REQUIRED)
include_directories (${Boost_INCLUDE_DIR} )
add_executable(reader uncompress.cpp)
target_link_libraries(reader ${Boost_LIBRARIES})
my cmake command is:
cmake -G "NMake Makefiles" .
This throws the error boost_zlib not found during cmake
Could not find the following Boost libraries:
boost_zlib
I have the following zlib library built into all paths are set properly:
NOTE if I change the CMakeLists.txt to this. it works fine. But I can't understand how :/
cmake_minimum_required (VERSION 2.8.9)
project (reader-demo CXX C)
add_executable(reader uncompress.cpp)
it works fine as expected. reader.exe is make and that works fine.
My question is: why is it not able to find boost_zlib the time I mention it in cmake and why is it working fine when I remove the target_link_libraries().
Expanding my comments to an answer:
zlib is not a registered Boost component although the Boost build process for boost_iostreams lib generates a boost_zlib and boost_bzip2 library. Both are internally generated and linked to. The CMake module FindBoost.cmake that is called when you add find_package(Boost COMPONENTS iostreams zlib REQUIRED) to your CMakeLists.txt file does not know about these libraries.
To answer the first part of the question:
remove zlib from your find_package call.
After doing so delete the CMakeCache.txt file because the values of a Boost search are cached in CMake.
To answer the second part of the question:
You removed the Boost stuff from the CMakeLists.txt but presumably did not delete the CMakeCache.txt. It might still contain the necessary Boost entries for the compiler/linker and that may be the reason why it works in the second call.
The libraries you showed in the image are the (multithreaded) static libraries and the import libraries for the DLLs.
To use the static libraries
you need to add set(Boost_USE_STATIC_LIBS ON) to your CMakeLists.txt file before calling find_package(Boost ...).
To use the Shared Objects (SO)/Dynamic Link Libraries (DLL)
you don't need to add anything to your CMakeLists.txt file before calling find_package(Boost ...), as dynamic link libraries are chosen by default. But to run the executable the directory that contains the DLLs must be added to the PATH environment variable.
NB:
Please remove the call to include_directories(${Boost_INCLUDE_DIR}) and add after the definition of your target
add_executable(reader uncompress)
target_include_directories(reader PRIVATE ${Boost_INCLUDE_DIRS})
Note the trailing S in the variable name. This variable is assigned a value in the find_package(Boost ..) call, the other variable is used to look up the include directory of Boost and is an input to the FindBoost module. Doing so you add the include directories to the target only but not to the global environment.
Finally you should add -DBOOST_ROOT=<path_to_your_Boost_installation> to your CMake command line call to make sure Boost is found regardless of a non standard installation directory.
For extended information about find_package(Boost ...) please see the documentation

CMake link library from subdirectory

I am trying to include SFML sources in my project. My directories are laid out like this:
main
SFML (subtree synced with the official git repo)
src
<various modules>
General (here lies the binary)
From the main level I am adding SFML subdirectory first and then src. As I've seen looking at the build log, this produces libraries:
sfml‑system
sfml‑window
sfml‑network
sfml‑graphics
sfml‑audio
sfml‑main
Now I want to link them to my binary in the General directory like this:
add_executable(main ${main_SRCS})
target_link_libraries (main
sfml‑system
sfml‑window
sfml‑network
sfml‑graphics
sfml‑audio
sfml‑main
# Other stuff here
)
But I get:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lsfml‑system
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lsfml‑window
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lsfml‑network
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lsfml‑graphics
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lsfml‑audio
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lsfml‑main
Why does CMake try to use system libraries instead of those it just built and how do I fix this?
This should just work.
Tried the following with Visual Studio generator on Windows and Makefile generator on Linux on CMake 3.2:
cmake_minimum_required(VERSION 2.8)
project(test)
add_subdirectory(SFML-2.2)
add_executable(foo bar.cpp)
target_link_libraries(foo sfml-system)
SFML is built correctly and foo links correctly to sfml-system.
The fact that you build your executable from another subdirectory should not have an impact here.