简体   繁体   English

为什么我的Boost库无法链接?

[英]Why does my Boost Library fail to link?

I'm using a MacBook Pro, running MacOS 10.13.1; 我正在使用运行MacOS 10.13.1的MacBook Pro; installed boost via homebrew using the following: 使用以下方法通过自制软件安装了Boost:

brew install boost --build-from-source

My code is the following: 我的代码如下:

#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;
}

The error I am receiving is the following: 我收到的错误如下:

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

I ran the code in the terminal using make and gcc 7.2.0 . 我使用makegcc 7.2.0在终端中运行了代码。

I attempted the following: 我尝试了以下操作:

g++ -std=c++17 -I /usr/local/include/ chkfile.cpp -o chkfile 

and got a new error: 并得到一个新的错误:

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

What am I doing wrong? 我究竟做错了什么?

Include filesystem this way 这样包含文件系统

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

Make sure you linked proper libraries 确保您链接了正确的库

-lboost_system -lboost_filesystem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM