为什么我的Boost库无法链接?
问题描述:
我正在使用运行MacOS 10.13.1的MacBook Pro;使用下面通过自制安装升压:为什么我的Boost库无法链接?
brew install boost --build-from-source
我的代码如下:
#include <iostream>
#include <boost/filesystem.hpp>
namespace boostfs = boost::filesystem;
int main(int argc, char* argv[])
{
if (argc <= 1)
{
std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
boostfs::path p(argv[1]);
if (boostfs::exists(p))
{
std::cout << "File " << p << " exists." << std::endl;
}
else
{
std::cout << "File " << p << " does not exist." << std::endl;
}
return 0;
}
我收到的错误是:
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&) in chkfile-c30777.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init.2 in chkfile-c30777.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in chkfile-c30777.o
___cxx_global_var_init.1 in chkfile-c30777.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [chkfile] Error 1
我在跑的代码终端使用make
和gcc 7.2.0
。
我尝试以下操作:
g++ -std=c++17 -I /usr/local/include/ chkfile.cpp -o chkfile
,并得到了新的错误:
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&) in cce7HDO8.o
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int) in cce7HDO8.o
"boost::system::generic_category()", referenced from:
boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const in cce7HDO8.o
boost::system::error_category::std_category::equivalent(std::error_code const&, int) const in cce7HDO8.o
__static_initialization_and_destruction_0(int, int) in cce7HDO8.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
我在做什么错?
答
包括文件系统这样
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
确保您链接正确的库
-lboost_system -lboost_filesystem
是你看到了什么错误?请将确切的文字复制并粘贴到问题中。 –
您使用了哪些命令来构建和链接代码?你是否在提示符处键入'make'并希望最好?大多数Makefiles默认回应他们正在运行的编译和链接命令 - 控制台中输出的内容是什么? – PaSTE
我刚添加了错误。另外,我只是从命令行运行'make'。 –