为什么我的 Boost 库无法 link?

Why does my Boost Library fail to link?

我使用的是 MacBook Pro,运行 MacOS 10.13.1;使用以下命令通过自制软件安装 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

我做错了什么?

以这种方式包含文件系统

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

确保链接了正确的库

-lboost_system -lboost_filesystem