win10下boost安装的三种方式,及cmake编译boost代码例子
一、exe安装的方式(不使用PCL点云库时,更推荐这种)
(1)该方式的下载地址为:
Boost C++ Libraries - Browse /boost-binaries at SourceForge.netFree peer-reviewed portable C++ source librarieshttps://sourceforge.net/projects/boost/files/boost-binaries/(2)以boost_1_72_0-msvc-14.2-64.exe为例,下载后双击即可安装;
(3)配置环境变量。
配置 BOOST_INCLUDEDIR 和 BOOST_LIBRARYDIR
和 PATH 中添加 D:\soft\boost\172\lib64-msvc-14.2
二、编译的方式(未做更多的测试)
(1)该方式的下载地址为:
Boost Version Historyhttps://www.boost.org/users/history/(2)这里我下载的是boost_1_72_0.zip(170 MB)
可参考:Windows VS2019编译Boost库学习记录 - JavaShuo,解压boost_1_72_0.zip后,双击bootstrap.bat文件,会生成一个b2.exe;
然后在cmd中输入(注意这里msvc-14.2指的是vs2019编译器,--prefix后面的路径是生成.h和.lib的放置路径)
b2.exe install --toolset=msvc-14.2 --build-type=complete --prefix=E:\boost172 link=shared runtime-link=shared runtime-link=static threading=multi debug release
(3)配置环境变量
配置 BOOST_INCLUDEDIR 和 BOOST_LIBRARYDIR
和 PATH 中添加 D:\2020software\common\boost172\boost_1_72_0\install0\lib
三、通过安装PCL点云库的第三方库的形式安装(PCL用户推荐,这样就不用重复安装,重复安装唯一目的就是需要boost的不同版本)
(1)去github的PCL仓库中下载名为“PCL-1.11.1-AllInOne-msvc2019-win64.exe”或者根据你的版本类似名字的文件,双击即安装了Boost、Eigen、VTK等多个第三方库;
(2)添加环境变量,配置BOOST_ROOT、BOOST_INCLUDEDIR 和 BOOST_LIBRARYDIR即可:
四、cmake编译boost代码的简单例子
(1)这里先说一下cmake的版本与boost版本的bug问题,参考:
c++ - CMake - New Boost version may have incorrect or missing dependencies and imported - Stack Overflowhttps://stackoverflow.com/questions/65560775/cmake-new-boost-version-may-have-incorrect-or-missing-dependencies-and-importeubuntu18 升级cmake - 漆天初晓 - 博客园升级boost库,cmake时报错: CMake Warning at /usr/share/cmake-3.10/Modules/FindBoost.cmake:801 (message): Newhttps://www.cnblogs.com/tyche116/p/13792577.html 于是我尝试了几个版本,最终用的cmake版本3.18.5.
(2)编译用的CMakeLists.txt,有两种写法,这里推荐第一种
第一种:
cmake_minimum_required(VERSION 3.1...3.20)
add_compile_options("/std:c++17")
project(Boost)
#Boost
set(Boost_DEBUG ON)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED COMPONENTS filesystem)
MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARY_DIRS = ${Boost_LIBRARY_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
include_directories(${Boost_INCLUDE_DIRS})
FILE(GLOB CPP_FILE0 ${PROJECT_SOURCE_DIR}/Boost-test/*.cpp)
add_executable(Boost_test ${CPP_FILE0})
target_link_libraries(Boost_test ${Boost_LIBRARIES})
第二种:
cmake_minimum_required(VERSION 3.1...3.20)
add_compile_options("/std:c++17")
project(Boost)
#Boost
set(Boost_DEBUG ON)
find_package(Boost REQUIRED)
MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARY_DIRS = ${Boost_LIBRARY_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
include_directories(${Boost_INCLUDE_DIRS})
FILE(GLOB CPP_FILE0 ${PROJECT_SOURCE_DIR}/Boost-test/*.cpp)
add_executable(Boost_test ${CPP_FILE0})
target_link_libraries(Boost_test ${Boost_LIBRARY_DIRS}/*.lib)
(3)main.cpp文件
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::path MyPath1{ "./a/b/c.jpg" };
std::string MyPath2 = MyPath1.string();
std::cout << MyPath2 << std::endl;
return 0;
}
更多推荐
所有评论(0)