2017-11-18 284 views
0

我正在使用運行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 

我在做什麼錯?

+0

是你看到了什麼錯誤?請將確切的文字複製並粘貼到問題中。 –

+0

您使用了哪些命令來構建和鏈接代碼?你是否在提示符處鍵入'make'並希望最好?大多數Makefiles默認迴應他們正在運行的編譯和鏈接命令 - 控制檯中輸出的內容是什麼? – PaSTE

+0

我剛添加了錯誤。另外,我只是從命令行運行'make'。 –

回答

0

包括文件系統這樣

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

確保您鏈接正確的庫

-lboost_system -lboost_filesystem