提升文件系统.is_directory永远不会工作

tay*_*ssy 15 c++ macos boost boost-filesystem

我一直在寻找高低,以解决这个问题,我似乎无法调试问题!

所以,这就是我所拥有的.

我通过自制软件(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
Run Code Online (Sandbox Code Playgroud)

请注意,这是由qt creator通过Add Library界面自动生成的.

这是我正在运行的代码永远不会有效.(意思是总是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;
}
Run Code Online (Sandbox Code Playgroud)

输出的结果总是system: 2来自boost :: system :: error_code

增加结果:

  • 尝试打印路径时,应用程序崩溃
  • 我用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;
 }
Run Code Online (Sandbox Code Playgroud)

我编译/运行

g++ test_boost.cpp -o main.out -lboost_system -lboost_filesystem
./main.out
Run Code Online (Sandbox Code Playgroud)

并且生成的响应是

Error Response: system:2
Run Code Online (Sandbox Code Playgroud)

很沮丧.试试旧版本

编辑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}  
Run Code Online (Sandbox Code Playgroud)

编辑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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

OwO*_*OwO 5

问题不在于您的代码,因为您的MCVE在在线编译器上编译并提供成功的输出。一种可能的解释是 Boost 是使用与您编译程序所用的标准库不同的标准库编译的,正如在基于 libstdc++ 的应用程序中使用 boost 1.55.0 时问题崩溃所暗示的那样??????. libc++并且libstdc++不是二进制兼容的,因为它们实现了字符串(现在可能不是这样,但过去确实是这样。)还要记住,在 Mac 上,g++可能被符号链接到clang++.

解决方案:

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

  • 如果您愿意,可以使用不同的 std 库从源代码重新编译 Boost