CMake - Unable to find boost components - c++

I try to start a project with mlpack in C++ on macOS 12.0.1 with an Intel-CPU.
For development I use CLion, gcc/g++ 11.2.0 (installed via Homebrew), Clion's bundled cmake 3.20.2 and mlpack 3.4.2 (also installed via Homebrew).
Building the project fails due to the following error:
CMake Error at /usr/local/lib/cmake/Boost-1.76.0/BoostConfig.cmake:141 (find_package):
Could not find a package configuration file provided by
"boost_program_options," (requested version 1.76.0) with any of the
following names:
boost_program_options,Config.cmake
boost_program_options,-config.cmake
Add the installation prefix of "boost_program_options," to
CMAKE_PREFIX_PATH or set "boost_program_options,_DIR" to a directory
containing one of the above files. If "boost_program_options," provides a
separate development package or SDK, be sure it has been installed.
Call Stack (most recent call first):
/usr/local/lib/cmake/Boost-1.76.0/BoostConfig.cmake:258 (boost_find_component)
/Applications/CLion.app/Contents/bin/cmake/mac/share/cmake-3.20/Modules/FindBoost.cmake:594 (find_package)
CMakeLists.txt:19 (find_package)
This error occurs even though I followed the instructions and defined the paths to boost_*_config.cmake. Here's the CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(cpp)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
include_directories(/usr/local/opt/armadillo/include)
include_directories(/usr/local/Cellar/mlpack/3.4.2_3/include/stb)
include_directories(/usr/local/opt/ensmallen/include)
include_directories(/usr/local/include)
include_directories(/usr/local/Cellar/mlpack/3.4.2_3/include/)
set(boost_program_options_DIR /usr/local/Cellar/boost/1.76.0/lib/cmake/boost_program_options-1.76.0/)
set(boost_math_c99_DIR /usr/local/Cellar/boost/1.76.0/lib/cmake/boost_math_c99-1.76.0)
set(boost_unit_test_framework_DIR /usr/local/Cellar/boost/1.76.0/lib/cmake/boost_unit_test_framework-1.76.0)
set(boost_serialization_DIR /usr/local/Cellar/boost/1.76.0/lib/cmake/boost_serialization-1.76.0)
set(boost_spirit_DIR /usr/local/Cellar/boost/1.76.0/lib/cmake/boost_spirit-1.76.0)
find_package(Boost 1.76.0 REQUIRED COMPONENTS program_options, math_c99, unit_test_framework, serialization, spirit)
message("${Boost_INCLUDE_DIRS}")
message("${Boost_LIBRARIES}")
include_directories(${Boost_INCLUDE_DIRS})
add_executable(cpp
cifar_train.cpp
cifar_eval.cpp)
target_link_libraries(cpp ${Boost_LIBRARIES})
The C++ code is just one of the mlpack examples, which can be found in their Github repository.
Since I'm relatively new to both CMake and C++ I can only guess what's wrong. To me it looks like something is not linked properly.

Related

Problems linking boost's filesystem in CMake

I'm using the filesystem library from Boost in this project (https://github.com/marcomedrano1999/files_in_directory.git) and it works fine when I link the boost root to the proyect (I'm using Visual Studio), but I need it to be cross-platform so I implement the following in my CMakeLists.txt:
cmake_minimum_required(VERSION "3.18.3")
project("listdir")
find_package(Boost 1.73.0 REQUIRED filesystem)
include_directories("${Boost_INCLUDE_DIRS}")
link_directories("${Boost_LIBRARY_DIRS}")
add_executable("${PROJECT_NAME}" "Main.cpp")
target_link_libraries("${PROJECT_NAME}" ${Boost_LIBRARIES})
but when I tried to configure it in CMake the following error displayed:
CMake Error at C:/Program Files/CMake/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
Could NOT find Boost (missing: filesystem) (found suitable version
"1.73.0", minimum required is "1.73.0")
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:458(_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.18/Modules/FindBoost.cmake:2177(find_package_handle_standard_args)
CMakeLists.txt:5 (find_package)
I'd tried several ways for this (as shown in the documentation page) but any of them have worked. Can someone tell me if I made something wrong?
If you have installed Boost library in a non-standard location, then you can specify the Boost Installation directory via BOOST_ROOT:
cmake -DBOOST_ROOT=<path to the boost installation directory> <... the rest of cmake options ...>
Also make sure that the filesystem part of boost is really installed (filesystem is a compiled library in contrast to other header only libraries).

CMake - Could NOT find Boost (missing: serialization) (found version "1.73.0"

I've been using Boost with my project for a while now, though until now, I'd only used the header-only libraries. I now want to use serialization, but when I try to add serialization as a REQUIRED component, I get the error written in the title.
Here is my CMAKE file:
cmake_minimum_required(VERSION 3.15)
project(GinRummyCPP)
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "D:/Program Files/boost/boost_1_73_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "D:/Program Files/boost/boost_1_73_0/libs")
set(CMAKE_CXX_STANDARD 17)
find_package(Boost COMPONENTS serialization)
add_executable(GinRummyCPP main.cpp)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(GinRummyCPP ${Boost_LIBRARIES})
endif()
I've checked my Boost folder, and serialization is at "D:\Program Files\boost\boost_1_73_0\libs\serialization"
I've tried setting BOOST_ROOT and other relevant variables, but nothing seems to work.
Here is the full error produced when loading the CMake file
CMake Warning at C:/Program Files/JetBrains/CLion 2019.3.2/bin/cmake/win/share/cmake-3.15/Modules/FindBoost.cmake:1144 (message):
New Boost version may have incorrect or missing dependencies and imported
targets
Call Stack (most recent call first):
C:/Program Files/JetBrains/CLion 2019.3.2/bin/cmake/win/share/cmake-3.15/Modules/FindBoost.cmake:1266 (_Boost_COMPONENT_DEPENDENCIES)
C:/Program Files/JetBrains/CLion 2019.3.2/bin/cmake/win/share/cmake-3.15/Modules/FindBoost.cmake:1904 (_Boost_MISSING_DEPENDENCIES)
CMakeLists.txt:7 (find_package)
-- Could NOT find Boost (missing: serialization) (found version "1.73.0")
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Documents/GinRummyCPP/cmake-build-release-mingw-64
Not sure if it's relevant, but I'm using CLion as my IDE and mingw-w64 as my compiler.
I ended up using a C++ package manager called vcpkg to install Boost:x64-windows, and Boost and the appropriate components were properly found after that. The instructions for how to install it can be found here: https://github.com/Microsoft/vcpkg
After installing it, 64 bit Boost can be installed with the following command:
.\vcpkg.exe install boost:x64-windows
I still don't know what the original issue with CMake was, and I'm sure there was a way to fix it without using a package manager, but this is a solution for those that are okay with using a package manager.
EDIT
Found out this still didn't completely resolve my issue, and led to other problems. I found a complete solution that doesn't use a package manager and posted it in my answer to this question: Undefined reference errors in simple boost serialization

Building basic C++ project with boost python v1.67 and cmake on Ubuntu

This is my first attempt to write a cmake file for a production level project, I'm just trying to locate the boost python(v 1.67.0) package using cmake.
I have a very small CMakeLists.txt.
cmake_minimum_required(VERSION 3.13)
find_package(Boost 1.67.0 REQUIRED COMPONENTS system python37)
if(Boost_FOUND)
message("Boost_FOUND")
endif()
It works fine if I just try to find system but fails when I add python37. As per the FindBoost documentation, I need to mention it the way I have mentioned it. This is the output that I get:
CMake Error at /usr/share/cmake-3.13/Modules/FindBoost.cmake:2100 (message):
Unable to find the requested Boost libraries.
Boost version: 1.67.0
Boost include path: /usr/include
Could not find the following Boost libraries:
boost_python37
Some (but not all) of the required Boost libraries were found. You may
need to install these additional Boost libraries. Alternatively, set
BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
to the location of Boost.
Call Stack (most recent call first):
CMakeLists.txt:2 (find_package)
-- Configuring incomplete, errors occurred!
When I run $ locate libboost_python I get the following output:
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.65.1
/usr/lib/x86_64-linux-gnu/libboost_python27.so.1.67.0
/usr/lib/x86_64-linux-gnu/libboost_python3-py37.so.1.65.1
/usr/lib/x86_64-linux-gnu/libboost_python37.so.1.67.0
Given this output, I assume that the package/library is present.
So, I tried to add the following line in CMakeLists.txt:
set(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu")
But the error remained. What am I missing here?
It looks like you are missing the dev package, as there is no /usr/lib/x86_64-linux-gnu/libboost_python27.so
Fix:
apt-get install libboost-python1.67-dev

Include cpp-netlib with CLion and Make on macOS

I'm very new to CLion, CMake and macOS at all so I need a little bit of help while trying to include cppnetlib into my CLion project.
I started out with creating an new CLion project which gave me a new CMakeLists.txt which I edited so that it would include boost as well:
CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
PROJECT(myapp)
SET(CMAKE_CXX_STANDARD 11)
SET(SOURCE_FILES main.cpp)
ADD_EXECUTABLE(myapp ${SOURCE_FILES})
FIND_PACKAGE(Boost 1.63.0 REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(dxcheck ${Boost_LIBRARIES})
I've installed boost through homebrew, but I did not find a package for cppnetlib with homebrew so I downloaded cppnetlib and placed it in /usr/local/Cellar/cpp-netlib. Now, when I follow the tutorial on their page and put
set (CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} /usr/local/Cellar/cpp-netlib)
find_package (cppnetlib 0.12.0 REQUIRED)
include_directories (${CPPNETLIB_INCLUDE_DIRS})
target_link_libraries (myapp ${CPPNETLIB_LIBRARIES})
into the CMakeLists.txt, I get the following output:
-- Boost version: 1.63.0
CMake Error at CMakeLists.txt:14 (find_package):
By not providing "Findcppnetlib.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"cppnetlib", but CMake did not find one.
Could not find a package configuration file provided by "cppnetlib"
(requested version 0.11.0) with any of the following names:
cppnetlibConfig.cmake
cppnetlib-config.cmake
Add the installation prefix of "cppnetlib" to CMAKE_PREFIX_PATH or set
"cppnetlib_DIR" to a directory containing one of the above files. If
"cppnetlib" provides a separate development package or SDK, be sure it has
been installed.
I've already done some research about finding packages but all I could achieve was that I was able to include cppnetlib by manually adding the include path to the CMakeLists.txt but then I got an error saying that the compiler could not find the headers which are located inside the "deps" folder in cpp-netlib.
I know want to know how to include a package like cppnetlib properly into my CLion project using CMake. I am using CLion 2017.1, macOS Sierra and I think CLion is using CMake 3.7.

How to use CMake's pkg_search_module() when compiling under Windows?

I'm programming a game which uses SDL2 and CMake.
In order to link and include SDL2 I use the following CMake code:
include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
target_link_libraries(MYLIB SDL2)
When compiling under Linux (Fedora) this works perfectly.
But what about Windows? There I don't have standard system locations for DLL/a files and include folders. There isn't even pkg-config.
This is the error I get:
Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
checking for one of the modules 'sdl2'
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.0/Modules/FindPkgConfig.cmake:425 (message):
None of the required 'sdl2' found
Call Stack (most recent call first):
.../CMakeLists.txt:4 (pkg_search_module)
I used the VS 2013 generator.
As you may already know, pkg_search_module() relies on the pkg-config program in order to locate dependencies. As you point out, Windows doesn't come with pkg-config. You might be able to install it and get things working that way, but probably a better alternative for crossplatform builds is to use CMake's find_package() function instead. This way you can eliminate the dependency on having pkg-config installed on the developer's machine.
cmake_minimum_required(VERSION 3.1)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")
find_package(SDL2 REQUIRED)
add_executable(sdl2demo sdl2demo.cpp)
target_include_directories(sdl2demo SYSTEM PRIVATE "${SDL2_INCLUDE_DIR}")
target_link_libraries(sdl2demo "${SDL2_LIBRARY}")
CMake doesn't come with a module for finding SDL2 so you'll need to add that to your source directory. That's why the above sets the CMAKE_MODULE_PATH; you add a cmake script that works cross platform for finding SDL2 to ${CMAKE_SOURCE_DIR}/cmake/Modules and then you can deliver that script with your source code.
The FindSDL2.cmake module I've used is: http://freerct.googlecode.com/svn/trunk/CMake/FindSDL2.cmake
This script should be able to locate SDL2 if it's installed in a standard location. If SDL2 isn't installed in a standard location or otherwise can't be located by this script, the developer has to configure the appropriate CMake variables to tell CMake the appropriate location, after which the configure and build will be able to procede normally.
cmake <source dir> -G "Visual Studio 12 2013 Win64" -DSDL2_INCLUDE_DIR=<sdl dir>/include -DSDL2MAIN_LIBRARY=<sdl dir>/lib/x64/SDL2main.lib -DSDL2_LIBRARY=<sdl dir>/lib/x64/SDL2.lib