本文介绍了CMake:在Windows上将Boost v1.69与MSVC一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Windows上的C++项目中使用Boost库="nofollow noreferrer"> CLion MSVC .我在此处下载了预编译的二进制文件,并将所有内容提取到了C:\local\boost_1_69_0_b1_rc3.我设置了以下CMakeLists.txt:

I'm trying to use the Boost library in my C++ project on Windows using CLion and MSVC. I downloaded the precompiled binaries here and extracted everything to C:\local\boost_1_69_0_b1_rc3. I setup the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.11.4)
project(BoostTesting)

set(CMAKE_CXX_STANDARD 17)

set(BOOST_ROOT "C:/local/boost_1_69_0_b1_rc3")
set(BOOST_LIBRARYDIR "C:/local/boost_1_69_0_b1_rc3/lib64-msvc-14.1")
set(BOOST_INCLUDEDIR "C:/local/boost_1_69_0_b1_rc3/boost")

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_DEBUG ON)
set(Boost_DETAILED_FAILURE_MSG ON)
set(Boost_COMPILER "-vc141")
set(Boost_ARCHITECTURE "-x64")

find_package(Boost 1.69.0 COMPONENTS filesystem REQUIRED)

if (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(BoostTesting main.cpp)
    target_link_libraries(BoostTesting ${Boost_LIBRARIES})
endif ()

使用CMake FindBoost 调试模式(Boost_DEBUG)我得到以下输出:

Using CMake's FindBoost debug mode (Boost_DEBUG) I got the following output:

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\BoostTesting
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Check for working 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
-- Check for working 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 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working 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
-- Check for working 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 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at CMakeLists.txt:18 (find_package):
  Policy CMP0074 is not set: find_package uses <PackageName>_ROOT variables.
  Run "cmake --help-policy CMP0074" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.

  Environment variable Boost_ROOT is set to:

    C:\local\boost_1_68_0\boost

  For compatibility, CMake is ignoring the variable.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1125 ] _boost_TEST_VERSIONS =
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1127 ] Boost_USE_MULTITHREADED = ON
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1129 ] Boost_USE_STATIC_LIBS = ON
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1131 ] Boost_USE_STATIC_RUNTIME = OFF
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1133 ] Boost_ADDITIONAL_VERSIONS =
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1135 ] Boost_NO_SYSTEM_PATHS =
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1203 ] Declared as CMake or Environmental Variables:
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1205 ]   BOOST_ROOT = C:/local/boost_1_69_0_b1_rc3
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1207 ]   BOOST_INCLUDEDIR = C:/local/boost_1_69_0_b1_rc3/boost
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1209 ]   BOOST_LIBRARYDIR = C:/local/boost_1_69_0_b1_rc3/lib64-msvc-14.1
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1211 ] _boost_TEST_VERSIONS =
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1286 ] Include debugging info:
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1288 ]   _boost_INCLUDE_SEARCH_DIRS = C:/local/boost_1_69_0_b1_rc3/boost;C:/local/boost_1_69_0_b1_rc3/include;C:/local/boost_1_69_0_b1_rc3;PATHS;C:/boost/include;C:/boost;/sw/local/include
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1290 ]   _boost_PATH_SUFFIXES =
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1310 ] location of version.hpp: C:/local/boost_1_69_0_b1_rc3/boost/version.hpp
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1334 ] version.hpp reveals boost 1.69.0
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1410 ] using user-specified Boost_COMPILER = -vc141
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1430 ] _boost_MULTITHREADED = -mt
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1506 ] _boost_RELEASE_ABI_TAG = -
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1508 ] _boost_DEBUG_ABI_TAG = -d
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1571 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = C:/local/boost_1_69_0_b1_rc3/lib64-msvc-14.1;C:/local/boost_1_69_0_b1_rc3/lib;C:/local/boost_1_69_0_b1_rc3/stage/lib;C:/local/boost_1_69_0_b1_rc3/lib;C:/local/boost_1_69_0_b1_rc3/../lib;C:/local/boost_1_69_0_b1_rc3/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib_boost_LIBRARY_SEARCH_DIRS_DEBUG   = C:/local/boost_1_69_0_b1_rc3/lib64-msvc-14.1;C:/local/boost_1_69_0_b1_rc3/lib;C:/local/boost_1_69_0_b1_rc3/stage/lib;C:/local/boost_1_69_0_b1_rc3/lib;C:/local/boost_1_69_0_b1_rc3/../lib;C:/local/boost_1_69_0_b1_rc3/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
CMake Warning 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:847 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:963 (_Boost_COMPONENT_DEPENDENCIES)
  C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1622 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:18 (find_package)


CMake Warning 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:847 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:963 (_Boost_COMPONENT_DEPENDENCIES)
  C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1622 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:18 (find_package)


-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1760 ] Searching for FILESYSTEM_LIBRARY_RELEASE: libboost_filesystem-vc141-mt-1_69;libboost_filesystem-vc141-mt;libboost_filesystem-mt-1_69;libboost_filesystem-mt;libboost_filesystem
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1813 ] Searching for FILESYSTEM_LIBRARY_DEBUG: libboost_filesystem-vc141-mt-d-1_69;libboost_filesystem-vc141-mt-d;libboost_filesystem-mt-d-1_69;libboost_filesystem-mt-d;libboost_filesystem-mt;libboost_filesystem
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1760 ] Searching for SYSTEM_LIBRARY_RELEASE: libboost_system-vc141-mt-1_69;libboost_system-vc141-mt;libboost_system-mt-1_69;libboost_system-mt;libboost_system
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1813 ] Searching for SYSTEM_LIBRARY_DEBUG: libboost_system-vc141-mt-d-1_69;libboost_system-vc141-mt-d;libboost_system-mt-d-1_69;libboost_system-mt-d;libboost_system-mt;libboost_system
-- [ C:/Users/User/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/183.4284.104/bin/cmake/win/share/cmake-3.12/Modules/FindBoost.cmake:1887 ] Boost_FOUND = 1
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.69.0

  Boost include path: C:/local/boost_1_69_0_b1_rc3

  Could not find the following static 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:18 (find_package)


-- Configuring incomplete, errors occurred!
See also "D:/Cpp/BoostTesting/cmake-build-debug/CMakeFiles/CMakeOutput.log".

[Failed to reload]

CMake正在查找那些特定文件,但我注意到-x64指示不是搜索文件名的一部分.我的目录仅包含64-bit个库:

CMake is looking for those specific files but I noticed that the -x64 indication is not part of the searched file names. My directory only contains 64-bit libraries:

为什么忽略Boost_ARCHITECTURE环境变量?我希望它可以将缺少的-x64添加到搜索的文件名中.如果我手动重命名libboost_filesystem-vc141-mt*.lib文件,则可以通过CMake找到它们.但是,如果我编译并链接了简单的main.cpp,则会收到链接器错误:

Why is the Boost_ARCHITECTURE environment variable ignored? I expect it to add the missing -x64 to the searched file names. If I manually rename the libboost_filesystem-vc141-mt*.lib files they can be found by CMake. However, if I compile and link my simple main.cpp I'm getting a linker error:

====================[ Build | BoostTesting | Debug ]============================
C:\Users\User\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\183.4284.104\bin\cmake\win\bin\cmake.exe --build D:\Cpp\BoostTesting\cmake-build-debug --target BoostTesting -- -j 4
Scanning dependencies of target BoostTesting
[ 50%] Building CXX object CMakeFiles/BoostTesting.dir/main.cpp.obj
[100%] Linking CXX executable BoostTesting.exe
CMakeFiles\BoostTesting.dir/objects.a(main.cpp.obj): In function `boost::filesystem::path_traits::convert(char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&)':
C:/local/boost_1_69_0_b1_rc3/boost/filesystem/path.hpp:997: undefined reference to `boost::filesystem::path::codecvt()'
C:/local/boost_1_69_0_b1_rc3/boost/filesystem/path.hpp:997: undefined reference to `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, int> const&)'
CMakeFiles\BoostTesting.dir/objects.a(main.cpp.obj): In function `boost::filesystem::file_size(boost::filesystem::path const&)':
C:/local/boost_1_69_0_b1_rc3/boost/filesystem/operations.hpp:616: undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\BoostTesting.dir\build.make:88: BoostTesting.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/BoostTesting.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/BoostTesting.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: BoostTesting] Error 2

main.cpp源代码:

#include <iostream>
#include <boost/filesystem.hpp>

using namespace boost::filesystem;

int main(int argc, char *argv[]) {
    if (argc < 2) {
        std::cout << "Usage: tut1 path\n";
        return 1;
    }
    std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
    return 0;
}

我很确定重命名文件不是正确的方法.但是即使这样,它也不起作用.我在做什么错?您如何在像这样的简单C++项目中正确使用Boost?

I'm pretty sure renaming the files is not the proper way of doing it. But even this way it doesn't work. What am I doing wrong and how do you properly use Boost in a simple C++ project like this one?

推荐答案

  1. 您收到CMake警告

  1. You got CMake warning

因为CMake版本没有为增强1.69做好准备(也许最新的CMake可以...).因此CMake会尽力识别该版本,因此它仍然可以工作(请参阅下面的第3点).

because that CMake version does not prepared for boost 1.69 (Maybe latest CMake does...).So CMake tries its best to recognize that version, so it still may work (see point 3 below).

@Felix Natter正确地指出,Boost_ARCHITECTURE仅从CMake 3.13处理.

@Felix Natter correctly pointed out above that Boost_ARCHITECTURE is handled only from CMake 3.13.

您可以使它无需手动重命名即可工作,无需GCC或MINGW ,如果您使用其中一个命名为"layouts"的文件重建增强,则CMake可以识别.(在您的调试日志中,这些是:

You can make it work without manual renaming, without GCC or MINGW, if you rebuild your boost with one of the file naming "layouts", your CMake recognizes.(From your debug log these ones:

libboost_<module name>-<toolset>[-mt]-<ABI tag>-<boost version>
libboost_<module name>-<toolset>[-mt]-<ABI tag>
libboost_<module name>[-mt]-<ABI tag>-<boost version>
libboost_<module name>[-mt]
libboost_<module name>)

libboost_<module name>-<toolset>[-mt]-<ABI tag>-<boost version>
libboost_<module name>-<toolset>[-mt]-<ABI tag>
libboost_<module name>[-mt]-<ABI tag>-<boost version>
libboost_<module name>[-mt]
libboost_<module name>)

不幸的是,只有system布局匹配,这意味着您需要在不同的"lib"文件夹中分离Debug和Release版本.有关布局"参数的说明,请参见此处.

Unfortunately only the system layout matches, which means you need to separate Debug and Release builds in different "lib" folders. For explanation about the "layout" parameter see here.

因此,通过指定--layout=system参数来重建升压,并且还可以通过指定variant=debugvariant=release来确定变型.

So rebuild boost by specifying the --layout=system parameter, and also decide the variant by specifying variant=debug or variant=release.

首先,在SF上您可以找到构建日志文件: https://sourceforge.net/项目/boost/文件/boost-binaries/1.69.0/boost_1_69_0-64bitlog.txt/下载
文件中的最后一个构建条目"是针对vc141的,它是您的版本:
b2 -j%NUMBER_OF_PROCESSORS% --without-mpi --build-type=complete toolset=msvc-14.1 address-model=64 architecture=x86 --prefix=.\ --libdir=lib64-msvc-14.1 --includedir=garbage_headers install

As a starting point, on SF you can find the build log file:https://sourceforge.net/projects/boost/files/boost-binaries/1.69.0/boost_1_69_0-64bitlog.txt/download
The last build "entry" in the file is for vc141, which is your version:
b2 -j%NUMBER_OF_PROCESSORS% --without-mpi --build-type=complete toolset=msvc-14.1 address-model=64 architecture=x86 --prefix=.\ --libdir=lib64-msvc-14.1 --includedir=garbage_headers install

转到您的增强源根目录,然后首先执行> bootstrap.确保检测到msvc-14.1.

Go to your boost source root and do > bootstrap first.Make sure msvc-14.1 was detected.

现在构建:
> b2 -a -j%NUMBER_OF_PROCESSORS% --without-mpi --with-filesystem toolset=msvc-14.1 address-model=64 architecture=x86 variant=debug --layout=system --prefix=C:\boost_build\debug

Now build:
> b2 -a -j%NUMBER_OF_PROCESSORS% --without-mpi --with-filesystem toolset=msvc-14.1 address-model=64 architecture=x86 variant=debug --layout=system --prefix=C:\boost_build\debug

在不同的目标目录中进行发布.根据需要更改--prefix.如果默认值不适合您,则还可以指定--libdir--includedir.当然,可能您需要修改CMakeList文件,以便它可以同时使用Debug和Release变体...

Do it for Release in a different target directory.Change --prefix according to your needs. You may also specify --libdir and --includedir if the default is not good for you.Of course, probably you need to modify your CMakeList file so that it can use both the Debug and Release variants...

因此,最好的方法是使用CMake版本,该版本可以识别增强版1.69,并且/或者获得基于CMake的版本的Boost,其中包括1.69.

So the best is to use a CMake version, which recognizes boost ver 1.69, and/or get a CMake-based version of boost, which includes 1.69.

这篇关于CMake:在Windows上将Boost v1.69与MSVC一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:11