0

我有一个 Python 扩展库 (pyd),为了能够调试它,我需要使用多线程 DLL (/MD) 运行时库进行编译。在我注入 Boost 依赖项之前,这很有效。 但我第一次调用 Boost 函数时,代码会因读取访问冲突异常而崩溃

在此处输入图像描述 在此处输入图像描述

经过一番调查,结果发现 cmake find_package(Boost) 使用不同的运行时库 ieboost_filesystem-vc140-mt-gd.lib 引入了 boost 库构建

我把它放在一个简单的测试用例中。

使用 vcpkg 构建 boost:
git clone https://github.com/Microsoft/vcpkg.git
.\bootstrap-vcpkg.bat
vcpkg install boost

这只会生成 -mt 和 -md-gd boost 库。

CMakeLists.txt

cmake_minimum_required (VERSION 3.6.3)
project (FindMDBoost CXX)
set (CMAKE_CXX_STANDARD 14)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MD")
file (GLOB_RECURSE SOURCES "source.cpp")

set(Boost_DEBUG ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTI_THREADED OFF)
find_package(Boost COMPONENTS filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

add_executable(testcode ${SOURCES})
target_link_libraries(testcode ${Boost_LIBRARIES})

源码.cpp

#include <boost/filesystem/path.hpp>
int main()
{   
    auto path = boost::filesystem::path("c:\\"); // Call  Boost to see if it crashes
}

运行 cmake:

mkdir build; cd build    
cmake .. "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake"

正如您在输出中看到的那样,cmake 仍然试图找到错误的运行时间并忽略了我的 Boost_USE_STATIC_LIBS 和 Boost_USE_MULTI_THREADED 标志!

-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1078 ] _boost_TEST_VERSIONS = 1.65.1;1.65.0;1.65;1.64.0;1.64;1.63.0;1.63;1.62.0;1.62;1.61.0;1.61;1.60.0;1.60;1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1080 ] Boost_USE_MULTITHREADED = TRUE
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1082 ] Boost_USE_STATIC_LIBS = 
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1084 ] Boost_USE_STATIC_RUNTIME = 
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1086 ] Boost_ADDITIONAL_VERSIONS = 
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1088 ] Boost_NO_SYSTEM_PATHS = 
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1156 ] Declared as CMake or Environmental Variables:
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1158 ]   BOOST_ROOT = 
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1160 ]   BOOST_INCLUDEDIR = 
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1162 ]   BOOST_LIBRARYDIR = 
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1164 ] _boost_TEST_VERSIONS = 1.65.1;1.65.0;1.65;1.64.0;1.64;1.63.0;1.63;1.62.0;1.62;1.61.0;1.61;1.60.0;1.60;1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1263 ] location of version.hpp: C:/dev/vcpkg/installed/x86-windows/include/boost/version.hpp
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1287 ] version.hpp reveals boost 1.66.0
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1363 ] using user-specified Boost_COMPILER = -vc140
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1383 ] _boost_MULTITHREADED = -mt
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1427 ] _boost_RELEASE_ABI_TAG = -
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1429 ] _boost_DEBUG_ABI_TAG = -gd
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1491 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = C:/dev/vcpkg/installed/x86-windows/lib;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH_boost_LIBRARY_SEARCH_DIRS_DEBUG   = C:/dev/vcpkg/installed/x86-windows/debug/lib;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1641 ] Searching for FILESYSTEM_LIBRARY_RELEASE: boost_filesystem-vc140-mt-1_66;boost_filesystem-vc140-mt;boost_filesystem-mt-1_66;boost_filesystem-mt;boost_filesystem
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:380 ]  Boost_LIBRARY_DIR_RELEASE = C:/dev/vcpkg/installed/x86-windows/lib _boost_LIBRARY_SEARCH_DIRS_RELEASE = C:/dev/vcpkg/installed/x86-windows/lib;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1692 ] Searching for FILESYSTEM_LIBRARY_DEBUG: boost_filesystem-vc140-mt-gd-1_66;boost_filesystem-vc140-mt-gd;boost_filesystem-mt-gd-1_66;boost_filesystem-mt-gd;boost_filesystem-mt;boost_filesystem
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:380 ]  Boost_LIBRARY_DIR_DEBUG = C:/dev/vcpkg/installed/x86-windows/debug/lib _boost_LIBRARY_SEARCH_DIRS_DEBUG = C:/dev/vcpkg/installed/x86-windows/debug/lib;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1641 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-vc140-mt-1_66;boost_system-vc140-mt;boost_system-mt-1_66;boost_system-mt;boost_system
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:380 ]  Boost_LIBRARY_DIR_RELEASE = C:/dev/vcpkg/installed/x86-windows/lib _boost_LIBRARY_SEARCH_DIRS_RELEASE = C:/dev/vcpkg/installed/x86-windows/lib;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1692 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-vc140-mt-gd-1_66;boost_system-vc140-mt-gd;boost_system-mt-gd-1_66;boost_system-mt-gd;boost_system-mt;boost_system
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:380 ]  Boost_LIBRARY_DIR_DEBUG = C:/dev/vcpkg/installed/x86-windows/debug/lib _boost_LIBRARY_SEARCH_DIRS_DEBUG = C:/dev/vcpkg/installed/x86-windows/debug/lib;NO_DEFAULT_PATH;NO_CMAKE_FIND_ROOT_PATH
-- [ C:/install/cmake-3.10.2-win64-x64/share/cmake-3.10/Modules/FindBoost.cmake:1767 ] Boost_FOUND = 1
-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   filesystem
--   system
-- Configuring done
-- Generating done
-- Build files have been written to: C:/dev/cpptests/FindMDBoost/build

这是链接阶段的输出

更新:我从头开始构建 boost 库,代码停止崩溃。所以这似乎是一个 vcpkg 问题,它没有构建我需要的 boost 版本。
b2 runtime-link=shared -j16
我的新 CmakeList.txt 文件

cmake_minimum_required (VERSION 3.6.3)
project (FindMDBoost CXX)
set (CMAKE_CXX_STANDARD 14)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MD")
file (GLOB_RECURSE SOURCES "source.cpp")

include_directories(C:/dev/boost/boost_1_66_0)

link_directories("C:/dev/boost/boost_1_66_0/stage/lib")
add_executable(testcode ${SOURCES})
target_link_libraries(testcode libboost_filesystem-vc141-mt-x32-1_66.lib)
4

2 回答 2

1

我相信这个问题与你如何告诉 vcpkg 在安装它时构建 boost 有关。您可以通过指定它们所谓的三元组来指定静态或动态链接。

所以对你来说:vcpkg install boost --triplet x64-windows-static

这将构建用于静态链接的 boost 库。

于 2018-04-13T03:22:02.037 回答
0

带有 -mt 的库版本是链接到 /MD 的正确版本;令人困惑的是,静态库会有一个 s,但从一开始的评论中看起来并不是你想要的。您正在加载 mt-gd 版本,它们只是这些库的调试版本;我想他们希望调用 python 也是调试版本。

于 2022-02-12T20:01:23.050 回答