【问题标题】:Why does my Boost Library fail to link?为什么我的 Boost 库无法链接?
【发布时间】:2017-11-18 03:02:27
【问题描述】:

我使用的是运行 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

我使用makegcc 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

我做错了什么?

【问题讨论】:

  • 您看到了什么错误?请将准确的文本复制并粘贴到问题中。
  • 您具体使用了哪些命令来构建和链接代码?您是否只是在提示符处输入make 并希望获得最好的结果?大多数 Makefiles 默认回显它们正在运行的编译和链接命令——控制台中的输出是什么意思?
  • 我刚刚添加了错误。另外,我只是从命令行运行make

标签: c++ gcc boost homebrew macos-high-sierra


【解决方案1】:

以这种方式包含文件系统

#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS

确保您链接了正确的库

-lboost_system -lboost_filesystem

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2017-05-15
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 2019-01-31
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多