Cannot find boost libraries with cmake - c++

I've install boost 1.63.0 on Windows and I am trying to build with CMake (using Visual Studio 2017 as a generator). I'm having trouble getting find_package() to find my boost libraries and I can't figure out why.
CMakeLists.txt:
find_package(Boost REQUIRED COMPONENTS system filesystem thread)
Output:
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.8/Modules/FindBoost.cm
ake:1813 (message):
Unable to find the requested Boost libraries.
Boost version: 1.63.0
Boost include path: C:/Program Files (x86)/boost/boost_1_63_0
Could not find the following Boost libraries:
boost_system
boost_filesystem
boost_thread
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.
Boost finds the includes, but not the libraries. Headers are located at:
%BOOST_ROOT%. Libraries are located at %BOOST_ROOT%/stage/lib. When I look at _boost_LIBRARY_SEARCH_DIRS_RELEASE, the first place it looks is the correct location. I've also tried hard-coding BOOST_LIBRARYDIR to that path just to be sure.
To install boost I extracted the downloaded archive to %BOOST_ROOT%, then ran bootstrap and .\b2 link=static,shared threading=single,multi. This should give me all versions of the libraries. In the case of boost:system, I have the following binaries in %BOOST_ROOT%/stage/lib%:
boost_system-vc100-mt-1_63.dll
boost_system-vc100-mt-1_63.lib
boost_system-vc100-mt-gd-1_63.dll
boost_system-vc100-mt-gd-1_63.lib
libboost_system-vc100-mt-1_63.lib
libboost_system-vc100-mt-gd-1_63.lib
I've tried enabling and disabling the following, but to no avail:
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED OFF )
set( Boost_DEBUG ON )
Here is an interesting part. The Boost_DEBUG parameter spits out this line:
Searching for SYSTEM_LIBRARY_RELEASE: boost_system-vc141-mt-1_63;boost_system-vc141-mt;boost_system-mt-1_63;boost_system-mt;boost_system
Note the vc141 versus vc100. I think .\b2 built something for vc100. That's strange because I was running it from the Dev Command Prompt for VS 2017. I've taken a wild guess and tried to build boost with ./b2 toolset=msvc-14.1 but I get an error: *** argument error * rule maybe-rewrite-setup ( toolset : setup-script : setup-options : version : rewrite-setup ? )".
How can I ensure that I compile boost with VS2017 or MSVC141?
This thread seems related:
Version numbers for Visual Studio 2017, Boost and CMake

Check the FindBoost.cmake script that is being used. Depending on the version of CMake you use, this version of Boost may not be handled. The dependencies between libraries is set depending on the version of Boost found.
For example the latest version of the script in CMake sources on GitHub handles version 1.63. I had the problem with CMake v3.6.2 which does not handle it.
Regarding the version mismatch for MSVC I don't know, sorry.

I compiled boost, and am compiling the linking application with the same toolset. Therefore I decided that it was safe to simply rename all compiled libs from *-vc100-* to *-vc141-*. While normally I would discourage that (you could get subtle differences in the ABI), in this case I was certain that it was the same compiler and so it's clear that either cmake or b2 had a bug which created(or searched) a file with the wrong name.
After doing that, cmake not only found boost, but linked successfully.

Related

Boost+CMake: no suitable build variant

I am trying to deploy some libraries on embedded SPARC-like machine for project in which I involved. I successfully built bleeding-edge Boost library on this machine and installed it into /util directory. Then I tried to write simple tester script for CMake. Here is the beginning of this script:
cmake_minimum_required(VERSION 2.8.3)
project(cpp_boost_test)
find_package(Boost REQUIRED COMPONENTS system thread)
find_package(console_bridge REQUIRED)
message(STATUS "Boost Includes: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost Libraries: ${Boost_LIBRARIES}")
While running I receive the following error message:
-- Found Boost 1.70.0 at /util/lib/cmake/Boost-1.70.0
-- Requested configuration: QUIET REQUIRED COMPONENTS
system;thread
-- Found boost_headers 1.70.0 at /util/lib/cmake/boost_headers-1.70.0
-- Found boost_system 1.70.0 at /util/lib/cmake/boost_system-1.70.0
-- No suitable boost_system variant has been identified!
-- libboost_system.so.1.70.0 (shared, BUILD_SHARED_LIBS not ON, set Boost_USE_STATIC_LIBS=OFF to override)
CMake Error at /util/lib/cmake/Boost-1.70.0/BoostConfig.cmake:60 (find_package):
Found package configuration file:
/util/lib/cmake/boost_system-1.70.0/boost_system-config.cmake
but it set boost_system_FOUND to FALSE so package "boost_system" is considered to be NOT FOUND.
Reason given by package:
No suitable build variant has been found.
Call Stack (most recent call first):
/util/lib/cmake/Boost-1.70.0/BoostConfig.cmake:89 (boost_find_dependency)
/util/share/cmake-3.7/Modules/FindBoost.cmake:229 (find_package)
CMakeLists.txt:4 (find_package)
I tried to set Boost_USE_STATIC_LIBS option but the error still remains.
Can this be a version conflict in Boost.System or not? What should I do to avoid this error? It is not possible to remove Boost from project, so I can not accept an answer about such removal...
I investigated the history of changes in Boost, so the answer is that building dependencies in projects linking Boost should be manually included into CMake (see answers here: CMake finds Boost but the imported targets not available for Boost version for details). Thus it is mandatory to use Boost version that is slightly older than CMake version you use. I solved my problem with rollback to Boost 1.61 (according to peculiarities of my embedded platform I can not compile CMake version above 3.7.0).
Check the version of your current cmake installation by using cmake --version.
It's highly likely greater than 3.0. Since there is an incompatibility between old cmake and modern cmake (>3.0), you can try to fix it by change the VERSION to your current cmake's version or any number >= 3.0.

Boost 1.69 and CMake 3.13

I have just updated my MSYS2 and received a new version of boost library 1.69.0. But after this update CMake 3.13.1 that must support this boost version says to me:
CMake Error at C:/Program Files/JetBrains/CLion 2018.2.6/bin/cmake/win/share/cmake-3.13/Modules/FindBoost.cmake:2100 (message):
Unable to find the requested Boost libraries.
Boost version: 1.69.0
Boost include path: D:/msys64/mingw64/include
Could not find the following Boost libraries:
boost_chrono
boost_filesystem
boost_system
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
Boost.
As i see CMake found boost 1.69.0 but not found Boost libraries. Why? With Boost 1.68.0 all works perfect.
PROBLEM SOLVED:
Today i cheked updates in MSYS and received an updated package
mingw-w64-x86_64-boost-1.69.0-2
After that CMake found all Boost 1.69.0 libraries succesfuly

cmake FindBoost not finding Boost libraries when building with MinGW on Windows

qtcreator supports both qmake and cmake for cross platform development and
qmake works fine for both msvc and MinGW builds on Windows. However, I'd prefer to support just one tool: cmake.
The CMakeLists.txt at via-httplib works fine on linux and when using cmake to build msvc on Windows.
However, when using cmake to build MinGW on Windows, I get the following error:
CMake Error at C:/Program
Files/CMake/share/cmake-3.13/Modules/FindBoost.cmake:2100 (message):
Unable to find the requested Boost libraries.
Boost version: 1.69.0
Boost include path: D:/DevLibraries/boost/boost_1_69_0
Could not find the following Boost libraries:
boost_system
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 Boost.
Both the BOOST_LIBRARYDIR and BOOST_ROOT environment variables are set and are found when using the Visual Studio 15 2017 Win64 generator and default native compilers
The error message indicates that when using the MinGW Makefiles and default native compilers, cmake finds the BOOST_ROOT environment variable but not BOOST_LIBRARYDIR. So it does not seem to be the same problem described here: Cmake doesn't find Boost
Is this a bug in FindBoost.cmake? Is there a "work around"?
It is an issue with the _boost_ARCHITECTURE_TAG in FindBoost.cmake, described on line 1518.
The tag was added to handle Boost 1.66.0 and later versions.
It is not setting the _boost_ARCHITECTURE_TAG for MinGW to -x64, so it is searching for boost library names like:
libboost_system-mgw73-mt-d-1_69.a
instead of:
libboost_system-mgw73-mt-d-x64-1_69.a
There is a workaround: set the cmake variable: Boost_ARCHITECTURE to -x64.
Note: setting Boost_ARCHITECTURE as an environment variable does not work, as FindBoost.cmake does not read Boost_ARCHITECTURE as an environment variable.

Compiling C++ with Boost 1.68.0 on Windows using CMake and MinGW

I want to use the Boost library on Windows but doing so has been troublesome. I downloaded the Windows package from here and extracted it to C:\Boost:
I added the following to my CMake file:
find_package(Boost 1.68 REQUIRED COMPONENTS filesystem)
# ...
target_link_libraries(MyExecutable ${Boost_LIBRARIES})
I'm getting the following CMake error:
C:\Users\User\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\183.4284.104\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/mingw32-make.exe" "-DCMAKE_C_COMPILER=C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-gcc.exe" "-DCMAKE_CXX_COMPILER=C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-g++.exe" -G "CodeBlocks - MinGW Makefiles" D:\Cpp\MyProject
CMake Error at C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:2048 (message):
Unable to find the requested Boost libraries.
Boost version: 1.68.0
Boost include path: C:/Boost
Could not find the following Boost libraries:
boost_filesystem
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:6 (find_package)
-- Configuring incomplete, errors occurred!
See also "D:/Cpp/MyProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Failed to reload]
It apparently cannot find filesystem but it's there in C:\Boost\boost\filesystem (here is the documentation on FindBoost).
How do I setup my CMake file to use Boost properly? I tried setting the Boost environment variables as well but it still didn't work:
SET (BOOST_ROOT "c:/Boost")
SET (BOOST_INCLUDEDIR "c:/Boost/boost")
SET (BOOST_LIBRARYDIR "c:/Boost/libs")
FIND_PACKAGE(Boost 1.68.0 REQUIRED COMPONENTS filesystem)
Also, Boost says that most things don't have to be compiled so I didn't do that.
It cannot find the library boost::filesystem. Because boost::filesystem is one of the few libraries that need to be compiled (all the ones that you have to specify in the find package command have to be compiled).
You need to build boost first:
./booststrap.sh
And then:
bjam
It picks up whatever compiler is available, so you may have to set the proper toolset manually.
Not completely related, but I think your link line is incorrect, instead of:
target_link_libraries(MyExecutable Boost::filesystem)
it should say:
target_link_libraries(MyExecutable ${Boost_LIBRARIES})
Boost_LIBRARIES is automatically defined once Boost is found, so it's for free.
The CMake FindBoost macro only fills Boost_Libraries if the find_package contains a COMPONENTS section and the macro actually finds those libraries on disk.
So here I'd be saying I need Boost to be found AND it must have filesystem
find_package(Boost 1.66.0 REQUIRED COMPONENTS filesystem)
The macro then tries to construct a bunch of potential filenames for the filesystem (taking into account if I want shared version, debug, multi-threaded etc.) and searches for those names under Boost_ROOT. If it doesn't find the file it may error and won't fill out Boost_Libraries.
If in doubt, add a line like this to your CMake prior to find_packages() to see what libs the macro is looking for and compare to what you actually have:
set (Boost_DEBUG ON)
For me I discovered that I had a Boost that placed an -x32 and -x64 architecture version of the libs, e.g. "libboost_system-mgw92-mt-x64-1_66.a". The macro wasn't filling in the architecture prefix into the filename it was looking for and failed.
I had to add a line like this to give the macro a clue which version I wanted:
set (Boost_ARCHITECTURE "-x64")
After that ${Boost_Libraries} expanded correctly and I was able to correctly use it to link.

Cmake 3.0.2 Cannot link debug and release libraries from a list

We are migrating to Cmake 3.0.2, Visual studio 2013 and building for 64 bits.
What used to work before with Cmake does not work now. I am appending different library names to a list and then want to link all libraries from the list to the project. For example:
include_directories("${EMU_EXTERNAL_DIR}/cgal/4.4/include")
link_directories("${EMU_EXTERNAL_DIR}/cgal/4.4/lib/x64")
list(APPEND ${PROJECT_NAME}_LIBRARIES
debug CGAL_Core-vc120-mt-gd-4.4
debug CGAL_ImageIO-vc120-mt-gd-4.4
debug CGAL-vc120-mt-gd-4.4
optimized CGAL_Core-vc120-mt-4.4
optimized CGAL_ImageIO-vc120-mt-4.4
optimized CGAL-vc120-mt-4.4
)
Afterwards I add more libraries the same way. And finally:
target_link_libraries(${PROJECT_NAME} ${${PROJECT_NAME}_LIBRARIES})
This leads to VisualStudio searching for the Release libraries while in Debug. If I include explicitely the path to the Release libs as well, then I get a VS error: "..cannot find lib 'debug.lib'". It seems to me that Cmake does not parse the libraries names and the optimized/debug keywords correctly.
Anyone had a similar problem or a clue what is going on? It's pretty confusing since this used to work before.
Thanks!