当前位置:首页 > Windows程序 > 正文

Windows下的Boost库编译

2021-05-23 Windows程序

标签:

编译与安装Boost_1.69.0

到Boost官网下载 boost_1_69_0.zip

下载Boost_1.69.0

把下载的 boost_1_69_0.zip 解压到 E:\source\boost_1_69_0 下;

编译Boost_1.69.0

执行其下的脚本文件bootstrap.bat,它会把复制windows下的编译环境的工具bjam.exe复制此目录下.
下面以program_options库为例, 其需要的编译命令如下:

bjam install --prefix="D:\libraries\boost" debug release link=static runtime-link=static threading=multi address-model=32 --with-program_options

prefix 指定安装的路径

debug release 编译目录版本

link 编译生成动态链接库/静态链接库, static为静态库 shared为动态库

runtime-link 链接C/C++运行时库

threading 单/多线程编译; multi多线程, single单线程

address-model 目标平台: 32位/64位

--with-program_options 需要编译的库,可修改成自己需要的库

※对于Boost库,最好是用到什么库再编译,否则编译时间需要很长
上面的命令执行完后,就会把生成的库文件复制到prefix指定的路径.

使用Boost中的program_options库 CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(HelloBoost) find_package(Boost REQUIRED COMPONENTS program_options) include_directories(${Boost_INCLUDE_DIR}) link_directories(${Boost_LIBRARY_DIRS}) FILE(GLOB SC *.cpp) add_executable(${PROJECT_NAME} ${SC})

调用find_package()查找Boost库及导入其子库program_options, 另还需要向工具导入头文件与库文件所在的路径.

main.cpp #include <boost/program_options.hpp> namespace po = boost::program_options; #include <iostream> using namespace std; int main(int argc, char** argv) { int compression = -1; po::options_description desc("Allow options"); desc.add_options() ("help,h", "produce help message") ("compression,c", po::value<int>(&compression)->default_value(0), "set compression level"); po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); } catch (std::exception& ex) { std::cout << ex.what() << std::endl; return 0; } if (vm.count("help")) { cout << desc << endl; return 1; } if (vm.count("compression")) { cout << "Compression level was set to " << compression << endl; } else { cout << "Compression level was not set." << endl; } return 0; }

add_options()后中的po::value<int>(&compression)作用是把命令行参数绑定及直接赋值到临时变量compression;
另default_value(0)为参数设置默认值,若不设置默认值则会强制参数为必填项.

Windows下的Boost库编译

标签:

原文地址:https://www.cnblogs.com/dilex/p/14726829.html

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70007.html