簡體   English   中英

cmake,使用 /MD 和 find_package(Boost) 構建

[英]cmake, build with /MD and find_package(Boost)

我有一個 Python 擴展名 (pyd) 的庫,為了能夠調試它,我需要使用多線程 DLL (/MD) 運行時庫進行編譯。 在我注入 Boost 依賴項之前,這很好用。 但是我第一次調用 Boost function 代碼崩潰並出現讀取訪問沖突異常

在此處輸入圖像描述 在此處輸入圖像描述

經過一番調查后發現 cmake find_package(Boost) 引入了使用不同運行時庫構建的 boost 庫 ieboost_filesystem-vc140-mt-gd.lib

我把它放在一個簡單的測試用例中。

使用 vcpkg 構建 boost:
git 克隆https://github.com/Microsoft/vcpkg.git
.\bootstrap-vcpkg.bat
vcpkg 安裝提升

這只會生成 -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"

正如您在 output 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

這是鏈接階段的output

更新:我從頭開始構建 boost 庫,代碼停止崩潰。 所以這似乎是一個 vcpkg 問題,它沒有構建我需要的 boost 版本。
b2 運行時鏈接=共享-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)

我相信問題只與您在安裝vcpkg時告訴其如何構建boost有關。 您可以通過指定靜態鏈接或動態鏈接來指定三元組

所以對您來說: vcpkg install boost --triplet x64-windows-static

這將構建用於靜態鏈接的boost庫。

帶有 -mt 的庫版本是鏈接到 /MD 的正確版本; 令人困惑的是 static 庫會有一個 s 但從一開始的評論中看起來並不是你想要的。 您加載的 mt-gd 版本只是這些庫的調試版本; 我想他們希望調用 python 也是調試版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM