代码之家  ›  专栏  ›  技术社区  ›  raggot

boost::filesystem::current\u path()返回空路径

  •  2
  • raggot  · 技术社区  · 6 年前

    我有一个C++程序,我需要当前路径来创建一个文件夹。我的可执行文件的位置是 /home/me/foo/bin . 这就是我所说的:

    //Here I expect '/home/me/foo/bin/', but get ''
    auto currentPath = boost::filesystem::current_path(); 
    
    //Here I expect '/home/me/foo/', but get ''
    auto parentPath = currentPath.parent_path();
    
    //Here I expect '/home/me/foo/foo2/', but get 'foo2/'
    string subFolder    = "foo2";
    string folderPath   = parentPath.string() + "/" + subFolder + "/";
    
    //Here I expect to create '/home/me/foo/foo2/', but get a core dump
    boost::filesystem::path boostPath{ folderPath};
    boost::filesystem::create_directories( boostPath); 
    

    我曾经在没有使用柯南的情况下,用以前版本的Boost(我相信是1.45)成功地运行过这个程序。我的机器上正常安装了Boost。我现在跑的时候堆芯掉了 create_directories( boostPath);

    两个问题:

    1. 为什么不呢 current_path() 为我提供实际路径,然后返回空路径?
    2. 即使current_path()未返回任何内容,为什么即使使用 sudo ? 我不是简单地在根目录下创建文件夹吗?

    编辑:

    cout 上述变量的输出在两行之间,而不是使用调试模式, 提供以下输出:

    currentPath: ""
    parentPath: ""
    folderPath: /foo2/
    Segmentation fault (core dumped)
    

    但是 有时

    currentPath: "/"
    parentPath: "/home/me/fooA�[oFw�[oFw@"
    folderPath: /home/me/fooA�[oFw�[oFw@/foo2/
    terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
      what():  boost::filesystem::create_directories: Invalid argument
    Aborted (core dumped)
    

    跑步 conan profile show default

    [settings]
    os=Linux
    os_build=Linux
    arch=x86_64
    arch_build=x86_64
    compiler=gcc
    compiler.version=5
    compiler.libcxx=libstdc++
    build_type=Release
    [options]
    [build_requires]
    [env]
    
    1 回复  |  直到 6 年前
        1
  •  5
  •   drodri    6 年前

    libcxx 用于依赖项中,以及用于构建应用程序的依赖项。

    在g++(linux)中,有两种标准库模式可供使用, libstdc++ ,没有C++ 11的支持, libstdc++11 libcxx数据库 .

    • libstdc++11库 默认为g++>=5,但这也取决于linux发行版。即使你在像ubuntu14这样的老发行版中安装了g++>=5,默认的 libcxx数据库 仍将是 ,显然要升级它而不中断是不容易的。在开源中使用的非常流行的CI服务(如travis CI)也使用了较旧的linux发行版,因此 图书馆++ 连锁是最流行的。

    • 是g++<5的默认值。

    图书馆++ ,即使是现代发行版中的现代编译器。您可以在第一次执行conan时读取默认配置文件,但也可以在 .conan/profiles/default ,或用 conan profile show default libcxx数据库 如果可能,将为每个编译器检测。

    因此,如果您不更改默认配置文件(建议在生产中使用您自己的配置文件),那么当您执行 conan install ,安装的依赖项是根据 . 请注意 柯南安装 在大多数情况下,它是独立于构建的,它只是下载、解压和配置所需的依赖项,以及请求的配置(来自默认概要文件)。

    那么,当你在建筑的时候,如果你没有改变 _GLIBCXX_USE_CXX11_ABI ,则可以使用系统编译器默认值,在本例中,

    有几种方法可以解决这个问题:

    • 使用构建应用程序 图书馆++ 我也是。确保定义 _GLIBCXX_USE_CXX11_ABI=0 .
    • 安装的依赖项 . 编辑要使用的默认配置文件 libstdc++11库 ,然后发布新的 柯南安装