2013-03-26 48 views
18

我想運行程序,使用示例代碼的Ubuntu 12.10 boost :: filesystem,但它不想構建。無法鏈接程序使用Boost.Filesystem

#include <iostream> 
#include <boost/filesystem.hpp> 
using namespace boost::filesystem; 
using namespace std; 

void fun(const string& dirPath); 
int main() 
{ 
    fun("/home"); 
    return 0; 
} 

void fun(const string& dirPath) 
{ 
    path p (dirPath); 

    if (exists(p)) 
    { 
     if (is_regular_file(p)) 
      cout << p << " size is " << file_size(p) << '\n'; 

     else if (is_directory(p))  
      cout << p << "is a directory\n"; 

     else 
      cout << p << "exists, but is neither a regular file nor a directory\n"; 
    } 
    else 
     cout << p << "does not exist\n"; 
} 

而且CMake的代碼:

project(tttest) 
cmake_minimum_required(VERSION 2.8) 
aux_source_directory(. SRC_LIST) 
add_executable(${PROJECT_NAME} ${SRC_LIST}) 
FIND_PACKAGE(Boost 1.53 COMPONENTS filesystem system REQUIRED) 
include_directories(${Boost_INCLUDE_DIR}) 
link_directories(${Boost_LIBRARY_DIR}) 
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES}) 

不幸的是它會產生錯誤

CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::exists(boost::filesystem::path const&)': 
main.cpp:(.text._ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' 
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::is_directory(boost::filesystem::path const&)': 
main.cpp:(.text._ZN5boost10filesystem12is_directoryERKNS0_4pathE[_ZN5boost10filesystem12is_directoryERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' 
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::is_regular_file(boost::filesystem::path const&)': 
main.cpp:(.text._ZN5boost10filesystem15is_regular_fileERKNS0_4pathE[_ZN5boost10filesystem15is_regular_fileERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' 
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)': 
main.cpp:(.text._ZN5boost10filesystem9file_sizeERKNS0_4pathE[_ZN5boost10filesystem9file_sizeERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)' 
collect2: error: ld returned 1 exit status 

這是什麼問題的原因以及如何解決呢?

+2

嘗試建立與自動檢測增壓版本:FIND_PACKAGE(升壓COMPONENTS filesystem system REQUIRED) – podshumok 2013-03-26 12:07:44

+0

我向標籤添加了cmake。這個問題似乎與cmake有關。 – drescherjm 2013-03-26 13:41:32

+1

您的CMake版本是否支持1.53版本?您可能想要打印出CMakeLists.txt中$ {Boost_LIBRARIES}包含的內容,以更好地理解正在發生的事情。 MESSAGE(狀態$ {Boost_LIBRARIES})應該這樣做。 – drescherjm 2013-03-26 13:44:44

回答

1

對於一些升壓模塊,您必須編譯庫鏈接它們(使用bootstrap.sh)。 在你的情況,你必須編譯和鏈接Filesystem,並且probalbly System

看一看here

例如:

  • ./bootstrap.sh(bjam的)
  • rm -rf bin.v2 stage(between 2 bjam commands)
  • ./bjam release toolset = gcc address-model = 64 cxxflags = -fPIC
  • ./bjam調試工具集= GCC地址模式= 64個CXXFLAGS = -fPIC

如果您在Windows上鍊接,您不必手動鏈接你的圖書館,因爲它們會自動使用編譯鏈接。在Linux上,你必須這樣做。

根據文檔,這些模塊需要你購買或修建圖書館:

  • Boost.Filesystem的
  • Boost.GraphParallel
  • 了Boost.Iostreams
  • Boost.MPI
  • 加速.ProgramOptions
  • Boost.Python
  • Boost.R egex
  • Boost.Serialization
  • Boost.Signals
  • Boost.System
  • Boost.Thread
  • Boost.Wave
+1

這是在Linux上,我希望通過ubuntu中的包管理器來安裝。 – drescherjm 2013-03-26 13:42:52

22

提高文件系統的是,有一些ABI問題Boost庫之一相對於由於C++ 0x或C++ 11引起的函數簽名更改而言。CF升壓票:https://svn.boost.org/trac/boost/ticket/6779

你有三種解決方法:

  1. 禁止C++ 11作用域的枚舉關注升壓包含在你的程序的頭文件用#include(CF http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/):

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

    但是這個解決方案不是一個完整的解決方案,我讀它並不適用於每個人。

  2. 構建BOOST與C++ 11的選項(你使用你的應用程序相同的選項):http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide

    我看也並不爲大家工作。

  3. 設置專用於您的應用程序的交叉編譯器,您可以在專用環境中重建所需的所有庫。這確保了連貫性,穩定性和更多的可維護性,當然也是推薦的解決方案。 我還沒有閱讀,如果它已經過測試 - 可能是的,可能它的工作原理。無論如何,現在在計算機科學中,交叉編譯已經很好掌握了。你會發現很多好的教程和支持。在Linux Gentoo中,他們擁有奇妙的sys-devel/crossdev軟件包,使其變得非常簡單。

在我自己的案例中,解決方案1已經解決了這個問題。一旦我遇到另一個,我將切換到解決方案3.所以,我還沒有測試過它。

+0

上面只是一個改進,如果你正在使用libboost低於1.51的使用需要使用 的#define BOOST_NO_SCOPED_ENUMS 的#include「升壓/ filesystem.hpp」 和#undef BOOST_NO_SCOPED_ENUMS – 2015-10-26 18:47:25

+2

注意,對於選項1,** **所有升壓包括絕已經定義了'BOOST_NO_CXX11_SCOPED_ENUMS',否則肯定會跟蹤ODR違規行爲。 – ildjarn 2016-01-26 15:53:20

5

您需要在鏈接時添加libboost_filesystem庫。或者如果你的應用程序是多線程的話,libboost_filesystem-mt。就像這樣:

g++ -o file -lboost_filesystem-mt source_file.cpp 
+0

如果你使用的CMake是 'target_link_libraries(my_executable $ {Boost_LIBRARIES})' – Adrian 2017-04-26 10:38:53

0

您需要添加以下庫:如果你使用一個Makefile

g++ -o file -lboost_system -lboost_filesystem sourcefile.cpp 

CC=gcc 
CXX=g++ 
PROG = program 
CXXFLAGS := -std=c++1y -g -Wall 
LDFLAGS = -L/usr/local/lib -L/usr/lib/x86_64-linux-gnu 
LIBS= -lboost_system -lboost_filesystem 

SRCS= main.cpp 

OBJS=$(subst .cpp,.o,$(SRCS)) 

all: $(OBJS) 
    $(CXX) $(CXXFLAGS) -o $(PROG) $(OBJS) $(LIBS) $(LDFLAGS)