我正在尝试使用 VS2019 在 Windows、Conda 中编译一个应用程序。
我的CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.10)
project(MyProject)
find_package(Boost REQUIRED COMPONENTS program_options)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(my_program po_demo.cpp)
target_link_libraries(my_program Boost::program_options)
我的示例应用程序:
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char** argv) {
try {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("input-file", po::value<std::string>(), "set input file")
("output-file", po::value<std::string>(), "set output file")
("verbose,v", "enable verbose output")
("optimization-level", po::value<int>()->default_value(0), "set optimization level")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << "Usage: program_name [options]\n";
std::cout << desc;
return 0;
}
} catch (const po::error& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
} catch (std::exception ex) {
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
catch (...) {
std::cerr << "Error unknown" << std::endl;
return 1;
}
return 0;
}
我创建一个环境(根据 miniforge3 提示):
conda create -n potest
conda activate potest
conda install cxx-compiler libboost-devel cmake make
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=%CONDA_PREFIX% -G "Unix Makefiles" ..
make
程序输出:
Error: bad allocation
CMake 报告它在环境中找到了 boost 库。 dumpbin 报告以下内容:
Dump of file my_program.exe
File Type: EXECUTABLE IMAGE
Image has the following dependencies:
boost_program_options.dll
MSVCP140D.dll
VCRUNTIME140D.dll
VCRUNTIME140_1D.dll
ucrtbased.dll
KERNEL32.dll
Summary
1000 .00cfg
2000 .data
4000 .idata
3000 .pdata
B000 .rdata
1000 .reloc
1000 .rsrc
1F000 .text
1000 .tls
问题出现在应用程序的第一行。 如果我从
po::options_description desc("Allowed options");
更改为 po::options_description desc;
那么我们将继续直到 po::notify
,错误将改为:Error: vector too long
。 另外它会弹出这个对话框:
我很困惑。 这应该有效吗? conda版本的boost和这个编译器之间有兼容性问题吗?