提高文件系统.Is_directory从来不起作用
Boost Filesystem. is_directory never works
我一直在到处寻找这个问题的答案,但我似乎无法调试出这个问题!
这就是我得到的。
我已经通过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
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用