使用Boost.Filesystem的不能链接程序 [英] Can't link program using Boost.Filesystem

查看:1569
本文介绍了使用Boost.Filesystem的不能链接程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行的程序,使用boost ::文件系统的样本code在Ubuntu 12.10,但它并不想建立。

I'm trying to run program, using sample code of boost::filesystem on Ubuntu 12.10, but it doesn't want to build.

#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的code:

And CMake code:

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})

不幸的是,生成错误

Unfortunately it generates errors

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

这是什么问题的原因以及如何解决呢?

What is the reason of this problem and how to solve it?

推荐答案

提高文件系统的是,有一些相对的函数签名改变ABI问题Boost库之一,由于C ++ 0x或C ++ 11。 CF升压票:<一href=\"https://svn.boost.org/trac/boost/ticket/6779\">https://svn.boost.org/trac/boost/ticket/6779

Boost filesystem is one of the Boost library that have some ABI problem relative to function signature change due to C++0x or C++11. cf Boost ticket : https://svn.boost.org/trac/boost/ticket/6779

您有三种解决方法:

1)禁止C ++ 11枚举范围在有关升压头文件包含在你的程序用#include(CF <一个href=\"http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/\">http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/):

1) Inhibit C++11 scoped enums in concerned Boost header files included in your programs with #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

但这个解决方案不是一个完整的,我看它不为大家工作。

But this solution is not a complete one and I read it does not work for everybody.

2)建立与C ++ 11的选项(用于您的应用程序)相同的选项BOOST:<一href=\"http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide/\">http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide/

2) Build BOOST with the C++11 option (the same options you use for your application) : http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide/

我也阅读它并不适用于每个人的工作。

I read also it does not work for everybody.

3)建立专门为你的应用程序,你重建你在一个专用的环境中所需的所有库一个交叉编译器。这保证了连贯性以及稳定性加上更多可维护性,而且肯定是在推荐的解决方案。
如果它已经过测试,我没有看到 - 或许是肯定的,而且很可能它的作品。无论如何,交叉编译很好,现在在计算机科学掌握。你会发现很多很好的教程和IT支持。在Linux中的Gentoo,他们有了不起的SYS-的devel / crossdev包,使得它很容易。

3) Set up a cross compiler dedicated to your application where you rebuild all the libraries you need in a dedicated environment. That ensures coherence plus stability plus more maintenability, and is certainly the solution to recommand. I have not read if it has been tested - probably yes, and probably it works. Anyway, cross compiling is well mastered now in computer science. You will find many good tutorials and support for it. In Linux Gentoo, they have the marvelous sys-devel/crossdev package that makes it very easy.

在我自己的情况下,解决方案1已经解决了这个问题。当我遇到一个又一个,我会切换到解决方案3。因此,我还没有测试它。

In my own case, solution 1 has solved the problem. As soon as I encounter another one, I will switch to solution 3. So, I have not yet tested it.

这篇关于使用Boost.Filesystem的不能链接程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆