Here is my CMakeLists.txt file
cmake_minimum_required(VERSION 3.8)
project("pi-calc" VERSION 1.0)
find_package(Boost REQUIRED COMPONENTS multiprecision)
add_executable(pi-calc main.cpp)
target_link_libraries(pi-calc PRIVATE Boost::boost Boost::multiprecision)
This is the main part of error message, removing CMake find_package failure Call Stacks
Could NOT find Boost (missing: multiprecision) (found version "1.67.0")
I have tried googling around for a solution but couldn't find anything.
Many of Boost's libraries are header-only libraries, including the multiprecision library. You only need to explicitly call out the libraries in COMPONENTS that are not header-only, shown in the list here.
If you need a header-only library, such as multiprecision, you will get this for free from the Boost::boost target, which includes all the Boost headers. There is no need to list any COMPONENTS:
cmake_minimum_required(VERSION 3.8)
project("pi-calc" VERSION 1.0)
find_package(Boost REQUIRED)
add_executable(pi-calc main.cpp)
target_link_libraries(pi-calc PRIVATE Boost::boost)
Note, in CMake versions 3.15 and later, you should instead use the Boost::headers target, which supersedes the Boost::boost target.
Related
I need to use custom build of boost lib, which uses own library naming prefix as well as namespace in the code.
I need to link with it in CMakeLists.txt:
set(BOOST_INCLUDEDIR /path/to/custom/boost/include)
set(BOOST_LIBRARYDIR /path/to/custom/boost/lib)
set(BOOST_NAMESPACE my_boost_161)
set(Boost_NO_SYSTEM_PATHS OFF)
set (Boost_USE_STATIC_LIBS OFF)
find_package (Boost REQUIRED COMPONENTS my_unit_test_framework)
include_directories (${Boost_INCLUDE_DIRS})
the libraries have a names like :
libmy_boost_161_unit_test_framework.so
and really exist in lib folder
there is a errors like:
CMake Error at
/home/user1/.localcmake/share/cmake-3.14/Modules/FindBoost.cmake:2132
(message): Unable to find the requested Boost libraries.
Boost version: 1.61.0
Boost include path:
/path/to/custom/boost/include
Could not find the following Boost libraries:
boost_unit_test_framework
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR
to the directory containing Boost libraries or BOOST_ROOT to the
location of
How can I specify to look for libs with names like (my_ addition to standard names):
similar to Boost_LIB_VERSION which gives an ability to add the version to lib name
my_boost_161_unit_test_framework
This addition to the CMake script fixes the problem:
...
set(Boost_NO_SYSTEM_PATHS OFF)
set(Boost_LIB_PREFIX my_boost_161)
set(Boost_NAMESPACE my_boost_161)
set(Boost_USE_STATIC_LIBS OFF)
...
This question is one that has occurred before on SO. For instance this question:
Cmake doesn't find Boost
But the answers there and elsewhere don't seem to work.
On Ubuntu 16.04 with the stock boost 1.58.0 installed, I have also built, in a custom location, boost 1.68.0.
Now I am trying to compile a simple c++ program using boost, with cmake. It does not find boost. Either version (although 1.68.0 is the one I really want to use).
It gives:
-- Could NOT find Boost (missing: Boost_DIR)
The CMakeLists.txt file is below. CMake 3.12.1 is being used.
cmake_minimum_required(VERSION 3.0.0)
set(CMAKE_CXX_STANDARD 17)
project(mytest CXX)
set(Boost_DEBUG ON)
set(Boost_DETAILED_FAILURE_MSG ON)
set(BOOST_ROOT /home/hal/projects/boost/boost)
# set(Boost_DIR /home/hal/projects/boost/boost)
#set(Boost_USE_DEBUG_LIBS ON)
#set(Boost_USE_STATIC_LIBS ON)
#set(Boost_USE_MULTITHREADED ON)
# set(Boost_USE_STATIC_RUNTIME OFF)
#set(Boost_INCLUDE_DIR /home/hal/projects/boost/boost )
set(Boost_ADDITIONAL_VERSIONS "1.58" "1.58.0")
#set(BOOST_INCLUDEDIR /home/hal/projects/boost/boost/include )
#set(BOOST_LIBRARYDIR /home/hal/projects/boost/boost/lib )
#SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "/home/hal/projects/boost/boost")
#SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "/home/hal/projects/boost/boost/lib")
find_package(Boost 1.68.0 COMPONENTS system date_time PATHS /home/hal/projects/boost/boost )
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(mytest main.cpp)
target_link_libraries(mytest ${Boost_LIBRARIES} stdc++)
endif()
Unless you work with implementing of searching packaging, option PATHS for find_package is not such useful, just remove it:
find_package(Boost 1.68.0 COMPONENTS system date_time)
Explanations
There are two ways for search packages in CMake:
With XXXConfig.cmake script, shipped with a specific package installation. In this script all paths (libraries, include directories, etc.) are hardcoded.
With FindXXX.cmake script, shipped with CMake itself. This script searches the libraries and headers under the system directories (like /usr/local/lib) but also takes hints from the user.
By default, the second way is tried; only if FindXXX.cmake script is absent, the first way is used.
But some options for find_package are applied only for the first way, and PATHS is exactly an option of this sort: it specifies paths where XXXConfig.cmake file can be found. With such options, find_package uses the second way - it tries to find XXXConfig.cmake script and execute it. But it seems that your Boost installation lacks of this config script, so CMake fails to find Boost.
i am using Jetbrains CLion 2017.3 and the bundled CMake version 3.9.6 with mingw64 5.0 version/g++ 7.1.
Although reading the "Mastering CMake" ( i am new to CMake !) i have many difficulties to understand the basics. Since 3 days i am searching for a CMake solution to create a own header-only library that uses the boost (1.66.0 ) libraries.
Using my CMakeLists.txt results in finding the boost libraries, but i cannot include boost headers in a header file from my current source directory.
My current source diretory contains the "CMakeLists.txt" and the header file
"test_boost.h".
If i try to include boost headers in the header file "test_boost.h", boost headers cannot be found !
What i am doing wrong ?
My CMakeLists.txt :
cmake_minimum_required(VERSION 3.9)
project(headerOnlyLib1)
set(CMAKE_CXX_STANDARD 11)
set(ENV{BOOST_ROOT} "C:/dev/boost/mingw/boost_1_66_0/boost")
set(Boost_USE_STATIC_LIBS ON) # only find static libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(BOOST 1.66 REQUIRED)
IF (Boost_FOUND)
message(STATUS "BOOST FOUND !")
ELSE()
message(STATUS "BOOST NOT Found !")
endif()
add_library(headerOnlyLib INTERFACE)
target_include_directories(headerOnlyLib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(headerOnlyLib INTERFACE ${Boost_INCLUDE_DIRS})
target_link_libraries(headerOnlyLib ${Boost_LIBRARIES})
Short answer: You can't.
A "header-only library" is just that, one or more headers, only. It's not something that is linked or really stand-alone.
If your header-only library have dependencies, then the users of your library also have those dependencies and need to include them in their own build.
I think that you can, but you need to be more specific in defining your boost dependencies.
For example, the CMakeLists.txt file here depends upon boost::system for boost::asio. The dependency is defined as follows:
find_package(Boost REQUIRED COMPONENTS system)
if(Boost_FOUND)
target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
# Boost::asio is header only but it requires Boost::system
target_link_libraries(${PROJECT_NAME} INTERFACE Boost::system)
.
.
.
endif(Boost_FOUND)
In your case, the target is Boost::boost for header-only dependencies, see FindBoost. So the relevant part becomes:
find_package(Boost 1.66 REQUIRED COMPONENTS boost)
IF (Boost_FOUND)
message(STATUS "BOOST FOUND !")
target_include_directories(headerOnlyLib INTERFACE ${Boost_INCLUDE_DIRS})
ELSE()
message(STATUS "BOOST NOT Found !")
endif()
I recommend watching Daniel Pfeifer's talk at C++ Now 2017 for more information.
A lot has changed since "Mastering CMake" was written...
I am trying to get a simple boost.log example running on Linux using GCC 4.4.5, CMake 2.8.2 and Boost 1.53.0.
Compiling boost and boost log succeeded, but I keep getting issues when linking my test program to boost.log.
I use the following CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
project(QuantibBoostLogTest)
# Include boost headers
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Threads)
find_package(Boost 1.53.0 COMPONENTS thread date_time filesystem system log log_setup REQUIRED)
if(Boost_FOUND)
include_directories( ${Boost_INCLUDE_DIRS} )
link_libraries(${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
else(Boost_FOUND)
message(FATAL_ERROR "Cannot build Quantib Boost Log test without Boost. Please set Boost_DIR.")
endif(Boost_FOUND)
add_executable(quantibBoostLogTest boost_log_test.cxx)
install(TARGETS quantibBoostLogTest DESTINATION .)
CMake does detect the boost libraries correctly, but I still get linker errors, mostly of the form:
core.cpp:(.text+0x1b0e): undefined reference to `boost::detail::get_tss_data(void const*)'
I do link the thread libraries. Does anybody know how to solve this?
It seems like boost.log depends on boost.thread library then you need change order of libraries. See why link order does matter
Try following order
find_package(Boost 1.53.0 COMPONENTS log log_setup thread date_time filesystem system REQUIRED)
if it will not help try include them two times as following
link_libraries(${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${Boost_LIBRARIES})
The linker error you give has something to do with either not linking against a native threading library like pthreads and/or boost_thread. (or both)
1)
From what I see you don't link against pthreads library.
By merely calling a CMake custom module that tries to find the library doesn't mean it'll also link against it.
Try and do:
SET(CMAKE_THREAD_PREFER_PTHREAD true)
FIND_PACKAGE (Threads)
IF(Threads_FOUND)
INCLUDE_DIRECTORIES(SYSTEM ${Threads_INCLUDE_DIR})
MESSAGE("Are we using pthreads? ${CMAKE_USE_PTHREADS_INIT}")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
ENDIF()
Check the FindThreads.cmake file of the CMake installation you have for more information regarding the use of the threads module.
You can usually find it in /usr/share/cmake-2.8/Modules/
2) Maybe the ordering of the linked Boost libraries is incorrect or the version you specified for Boost is invalid.
Try changing the boost version or don't specify it at all or change the order of the linked libraries
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost 1.53.0 COMPONENTS **system thread filesystem date_time log log_setup** REQUIRED)
IF(Boost_FOUND)
INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIR})
LINK_DIRECTORIES(${Boost_LIBRARY_DIR})
MESSAGE("Boost information")
MESSAGE("Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
MESSAGE("Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
MESSAGE("Boost Libraries: ${Boost_LIBRARIES}")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES})
ENDIF()
(The second contention might be completely wrong as I think the ordering of the elements specified after COMPONENTS in FIND_PACKAGE doesn't matter)
I've got both the static and the dynamic versions of the boost libraries in /usr/lib. Now I'd like CMake to prefer the static versions during the linkage of my executable. What can I do?
In your CMakeLists.txt file:
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED ...)
Where I have ..., you optionally put the names of the libraries you want to use, and then target_link_libraries(targetname ${Boost_LIBRARIES}) later below. If you have a fairly recent distribution of CMake, it should work exactly as advertised. I do it exactly this way in my own projects.
Here is a full example of CMAKEFILE,For example, include boost program options
cmake_minimum_required(VERSION 3.15)
project(your_project)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.70 COMPONENTS program_options REQUIRED)
set(CMAKE_CXX_STANDARD 14)
add_executable(your_project main.cpp)
target_link_libraries(rconpp Boost::program_options)
references:
cmake documents about BOOST