fferri
fferri

Reputation: 18950

How to find Boost libraries with cmake and Visual Studio 2017

I compiled Boost 1.65.1 with Visual Studio 2017.

When compiling some C++ project using cmake the Boost libraries are not found:

cmake -G"Visual Studio 15 2017" -DBoost_COMPILER="-vc150" ..

CMake Error at cmake-3.9/Modules/FindBoost.cmake:1900 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.65.1

  Boost include path: C:/Boost/include/boost-1_65_1

  Could not find the following Boost libraries:

          boost_thread
          boost_system
          boost_regex
          boost_timer
          boost_filesystem
          boost_serialization

  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:18 (find_package)

the libraries are there:

dir c:\Boost\lib
 Volume in drive C has no label.

 Directory of c:\Boost\lib

28/11/2017  12:47    <DIR>          .
28/11/2017  12:47    <DIR>          ..
...
28/11/2017  12:12           937.796 libboost_filesystem-vc150-mt-1_65_1.lib
28/11/2017  12:40         6.792.370 libboost_filesystem-vc150-mt-gd-1_65_1.lib
28/11/2017  12:20           996.592 libboost_filesystem-vc150-mt-s-1_65_1.lib
28/11/2017  12:17         5.951.092 libboost_filesystem-vc150-mt-sgd-1_65_1.lib
28/11/2017  12:28           996.456 libboost_filesystem-vc150-s-1_65_1.lib
28/11/2017  12:25         5.950.956 libboost_filesystem-vc150-sgd-1_65_1.lib
...
28/11/2017  12:13        13.418.950 libboost_regex-vc150-mt-1_65_1.lib
28/11/2017  12:40        43.663.182 libboost_regex-vc150-mt-gd-1_65_1.lib
28/11/2017  12:21        13.487.852 libboost_regex-vc150-mt-s-1_65_1.lib
28/11/2017  12:17        42.108.148 libboost_regex-vc150-mt-sgd-1_65_1.lib
28/11/2017  12:28        13.487.546 libboost_regex-vc150-s-1_65_1.lib
28/11/2017  12:25        42.107.842 libboost_regex-vc150-sgd-1_65_1.lib
28/11/2017  12:16         9.426.284 libboost_serialization-vc150-mt-1_65_1.lib
28/11/2017  12:43        33.389.696 libboost_serialization-vc150-mt-gd-1_65_1.lib
28/11/2017  12:24        10.690.636 libboost_serialization-vc150-mt-s-1_65_1.lib
28/11/2017  12:20        34.091.376 libboost_serialization-vc150-mt-sgd-1_65_1.lib
28/11/2017  12:31        10.690.106 libboost_serialization-vc150-s-1_65_1.lib
28/11/2017  12:27        34.090.846 libboost_serialization-vc150-sgd-1_65_1.lib
...
             200 File(s)  2.184.720.662 bytes
               2 Dir(s)  48.071.262.208 bytes free

How to use Boost in this configuration?

EDIT:

I see that there is a naming problem. From FindBoost.cmake:

#   Boost_USE_MULTITHREADED  - Set to OFF to use the non-multithreaded
#                              libraries ('mt' tag).  Default is ON.
#   Boost_USE_STATIC_LIBS    - Set to ON to force the use of the static
#                              libraries.  Default is OFF.
#   Boost_USE_STATIC_RUNTIME - Set to ON or OFF to specify whether to use
#                              libraries linked statically to the C++ runtime
#                              ('s' tag).  Default is platform dependent.
#   Boost_USE_DEBUG_RUNTIME  - Set to ON or OFF to specify whether to use
#                              libraries linked to the MS debug C++ runtime
#                              ('g' tag).  Default is ON.
#   Boost_USE_DEBUG_PYTHON   - Set to ON to use libraries compiled with a
#                              debug Python build ('y' tag). Default is OFF.
#   Boost_USE_STLPORT        - Set to ON to use libraries compiled with
#                              STLPort ('p' tag).  Default is OFF.
#   Boost_USE_STLPORT_DEPRECATED_NATIVE_IOSTREAMS
#                            - Set to ON to use libraries compiled with
#                              STLPort deprecated "native iostreams"
#                              ('n' tag).  Default is OFF.
#   Boost_COMPILER           - Set to the compiler-specific library suffix
#                              (e.g. "-gcc43").  Default is auto-computed
#                              for the C++ compiler in use.  A list may be
#                              used if multiple compatible suffixes should
#                              be tested for, in decreasing order of
#                              preference.
#   Boost_THREADAPI          - Suffix for "thread" component library name,
#                              such as "pthread" or "win32".  Names with
#                              and without this suffix will both be tried.
#   Boost_NAMESPACE          - Alternate namespace used to build boost with
#                              e.g. if set to "myboost", will search for
#                              myboost_thread instead of boost_thread.

there is no mention of 'gd' or 'sgd' tag, but my libs are named like that. I tried also to disable all options that could use those tags, but it is still failing:

 cmake -G"Visual Studio 15 2017" -DBoost_COMPILER="-vc150" -DBOOST_LIBRARYDIR=/c/Boost/lib -DBoost_USE_STATIC_RUNTIME=OFF -DBoost_USE_DEBUG_RUNTIME=OFF ..

CMake Error at cmake-3.9/Modules/FindBoost.cmake:1900 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.65.1

  Boost include path: C:/Boost/include/boost-1_65_1

  Could not find the following Boost libraries:

          boost_thread
          boost_system
          boost_regex
          boost_timer
          boost_filesystem
          boost_serialization

  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:18 (find_package)

Upvotes: 4

Views: 1481

Answers (1)

Leslie N
Leslie N

Reputation: 115

About the library naming:

Boost library (.lib,.dll on Win, .a,.so on Linux) names have 3 forms. They depend on the --layout option which was passed to b2 when boost was built. On Windows the default is versioned, which produces the file names you had. See my detailed explanation here.

Possible solutions to your problem:

  1. In your 2nd try you called
    cmake -G"Visual Studio 15 2017" -DBoost_COMPILER="-vc150" -DBOOST_LIBRARYDIR=/c/Boost/lib ...
    However I'm not sure the path /c/Boost/lib is valid for Windows CMake, please try C:\Boost\lib.
    BTW, instead of BOOST_LIBRARYDIR I would define simply BOOST_ROOT=C:\Boost.
  2. Since you have only libs and NOT DLLs, make sure that Boost_USE_STATIC_LIBS is ON in your CMake file (or passed on the command line). If you happen to use python and/or numpy libraries, too, then you need to define BOOST_PYTHON_STATIC_LIB and BOOST_NUMPY_STATIC_LIB, as well, for the compiler, when you include headers from these libs (e.g: add_definitions(-DBOOST_PYTHON_STATIC_LIB -DBOOST_NUMPY_STATIC_LIB)).
  3. Turn on boost diagnostics: -DBoost_DEBUG=ON
    CMake file print lot of info about how it tries to process boost.

Note:
Normally you don't need to set the Boost_USE_MULTITHREADED, etc. variables, since these are detected by CMake. From your "lib" directory file list, I can see that both debug and release, dynamically and statically linked C run-time versions of the library files were present.
See here about these letters in the library file names.

I hope some of these helped.

Upvotes: 3

Related Questions