提高文件系统.Is_directory从来不起作用

Boost Filesystem. is_directory never works

本文关键字:不起作用 directory 文件系统 Is      更新时间:2023-10-16

我一直在到处寻找这个问题的答案,但我似乎无法调试出这个问题!

这就是我得到的。

我已经通过homebrew (Mac OSX Mavericks)版本1.55.0安装了boost。

其他boost库工作得很好,但是boost::filesystem似乎无法与实际的文件系统交互。

这就是我如何链接它(使用QT)

macx: LIBS += -L$$PWD/../../../../../usr/local/Cellar/boost/1.55.0_1/lib/ -lboost_system-mt
macx: LIBS += -L$$PWD/../../../../../usr/local/Cellar/boost/1.55.0_1/lib/ -lboost_filesystem-mt
INCLUDEPATH += $$PWD/../../../../../usr/local/Cellar/boost/1.55.0_1/include
DEPENDPATH += $$PWD/../../../../../usr/local/Cellar/boost/1.55.0_1/include
macx: PRE_TARGETDEPS += $$PWD/../../../../../usr/local/Cellar/boost/1.55.0_1/lib/libboost_filesystem.a
macx: PRE_TARGETDEPS += $$PWD/../../../../../usr/local/Cellar/boost/1.55.0_1/lib/libboost_system.a

请注意,这是qt creator通过Add Library接口自动生成的。

这是我运行的代码,从来没有工作过。(意思是isDir总是false)

namespace fs = boost::filesystem;
boost::system::error_code c;
fs::path path("/path/to/some/dir"); // Have tried '.' '/' './' '/usr' Everything!
bool isDir = boost::filesystem::is_directory(path, c);
if(!isDir) {
    std::cout << "Error Response: " << c << std::endl;
    ui->directoryEdit->setStyleSheet("background-color: red;");
}
else {
    std::cout << "Is a directory!" << std::endl;
}

从boost::system::error_code

输出的结果总是system: 2

add Findings:

  • 当试图打印路径时,应用程序崩溃
  • 我用c++11重新编译了boost,而不是标准,但没有改变。

我肯定我错过了一些明显的东西,但我承认我需要帮助。

编辑:

所以我把它分离到一个main.cpp文件中:

#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>
#include <iostream>
#include <string>
int main(int argc, char *argv[]) {
    namespace fs = boost::filesystem;
    boost::system::error_code c;
    fs::path path("."); // Have tried '.' '/' './' '/usr' Everything!
    bool isDir = boost::filesystem::is_directory(path, c);
    if(!isDir) {
        std::cout << "Error Response: " << c << std::endl;
    }
    else {
        std::cout << "Is a directory!" << std::endl;
    }
    return 0;
 }

和我编译/运行

g++ test_boost.cpp -o main.out -lboost_system -lboost_filesystem
./main.out
而生成的响应是
Error Response: system:2

非常令人沮丧。要尝试使用旧版本

编辑2:

评论中的每个请求都是/usr/local/Cellar/boost/1.55.0_1/INSTALL_RECEIPT.json

{"compiler":"clang","HEAD":"4bf4ee77fa858bb5e56406504cf86564bd5ece3d","built_as_bottle":true,"stdlib":"libcxx","tapped_from":"Homebrew/homebrew","unused_options":["--with-mpi","--c++11","--with-icu","--without-static","--with-python","--universal","--without-single"],"poured_from_bottle":true,"used_options":[],"time":1399557362}  

编辑3:

g++版本:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
c++版本:

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix

问题不在于您的代码,因为您的MCVE在在线编译器上编译并给出成功的输出。一种可能的解释是,Boost是用与您正在编译的程序不同的标准库编译的,正如在Mac上基于libstdc++的应用程序中使用Boost 1.55.0时出现的问题所表明的那样。libc++libstdc++不是二进制兼容的,因为它们的string实现(现在可能不是这样,但在过去肯定是这样的)。另外请记住,在Mac上,g++可能被符号链接到clang++

解决方案:

  • 使用与Boost相同的std库编译程序。试试clang++ -stdlib=libc++ ...clang++ -stdlib=libstdc++ ...

  • 使用不同的std库从源代码重新编译Boost,如果你喜欢

如果/path/to/some/dir不是一个常规目录(例如它是一个符号链接),那么is_directory不需要返回true,即使符号链接指向目录

看一下

  • http://www.boost.org/doc/libs/1_55_0/libs/filesystem/doc/reference.html is_symlink
  • http://www.boost.org/doc/libs/1_55_0/libs/filesystem/doc/reference.html is_other