Xcode 10.2现在包含<filesystem>标头。但是,使用编写代码时std::filesystem,我遇到了许多额外的链接错误。
Undefined symbols for architecture x86_64:
"std::__1::__fs::filesystem::path::__filename() const", referenced from:
std::__1::__fs::filesystem::path::filename() const in dump_zip.cpp.o
std::__1::__fs::filesystem::path::filename() const in libxp_parse.a(zipped.cpp.o)
std::__1::__fs::filesystem::path::remove_filename() in libxp_parse.a(zipped.cpp.o)
std::__1::__fs::filesystem::path::has_filename() const in libxp_parse.a(zipped.cpp.o)
std::__1::__fs::filesystem::path::has_filename() const in libxp_parse.a(acf.cpp.o)
Run Code Online (Sandbox Code Playgroud)
我认为需要链接一个额外的库以支持文件系统,但是我无法确定它的位置或位置。知道这叫什么吗?
编辑:libc ++表示这libc++fs是必需的-但是,它似乎并未随Xcode一起分发,至少没有在默认搜索目录中分发。
我正在使用std :: filesystem编写一个简单的文件选择器。当前目录的条目存储在向量中。当我尝试使用std :: sort对向量进行排序时,程序崩溃。
在Ubuntu 19.04上使用g ++-9会发生这种情况。该文件是使用-D_GLIBCXX_DEBUG和-D_GLIBCXX_DEBUG_PEDANTIC调试标志编译的。
相关代码如下:
#include <filesystem>
#include <vector>
#include <algorithm>
namespace fs = std::filesystem;
struct FileBrowser {
std::vector<fs::path> files;
FileBrowser() {
UpdateFiles();
}
void UpdateFiles() {
files.clear();
for (const auto& entry : fs::directory_iterator(currentPath)) {
files.push_back(entry.path());
}
std::sort(files.begin(), files.end(), [](const auto& lhs, const auto& rhs) {
if (fs::is_directory(lhs) && !fs::is_directory(rhs)) {
return 1;
} else if (fs::is_directory(rhs) && !fs::is_directory(lhs)) {
return 0;
} else {
return lhs.filename().string().compare(rhs.filename().string());
}
});
}
};
Run Code Online (Sandbox Code Playgroud)
错误消息如下所示:
/usr/include/c++/9/debug/safe_iterator.h:294:
In function:
__gnu_debug::_Safe_iterator<_Iterator, …Run Code Online (Sandbox Code Playgroud) 状态的cppreference页面std::filesystem::path:
路径可以隐式转换为from和from
std::basic_strings,因此可以将它们与over files API一起使用,例如,作为std::ifstream::open
现在,转换为a std::filesystem::path很容易,因为它具有采用std::string类型的非显式构造函数。但是,我似乎找不到的是一种std::string隐式地进行查找的方法。
有一个string功能,但是std::string string() const;没有operator std::string()。使用
#include <filesystem>
void foo(std::string) {}
int main()
{
namespace fs = std::filesystem;
fs::path p1;
foo(p1);
}
Run Code Online (Sandbox Code Playgroud)
这段代码可以使用icc,gcc和clang很好地编译,但是不能使用MSVS编译,但会给出错误:
example.cpp
<source>(10): error C2664: 'void foo(std::string)': cannot convert argument 1 from 'std::filesystem::path' to 'std::string'
<source>(10): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot …Run Code Online (Sandbox Code Playgroud) 运行这个程序
#include <iostream>
#include <filesystem>
#include <vector>
using namespace std;
namespace fs = filesystem;
int main() {
vector<fs::path> paths{"a.o", "b.o"};
vector<const char *> argv{};
for (auto &p : paths) {
argv.push_back(p.string().data()); // line A
}
argv.push_back(paths[0].string().data());
argv.push_back(paths[1].string().data());
for (auto &s : argv) {
cout << s << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
得到
b.o
b.o
a.o
b.o
Run Code Online (Sandbox Code Playgroud)
为什么 argv 的第一个元素不是“ao”?
我尝试在A线断,找出当“博”是的push_back()插入的argv,argv的的第一个元素改变从“AO”到“博”。
然后,当我将 A 行更改为
argv.push_back(p.string().c_str()); // line A: .string().data() -> .string().c_str()
Run Code Online (Sandbox Code Playgroud)
结果一样。
当我将 A 行更改为
argv.push_back(p.c_str()); // line …Run Code Online (Sandbox Code Playgroud) 我无法使用 c++ 17 clang 编译此官方 cpp 文件系统参考示例:
https://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::current_path(fs::temp_directory_path());
fs::create_directories("sandbox/a/b");
std::ofstream("sandbox/file1.txt");
fs::create_symlink("a", "sandbox/syma");
// Iterate over the `std::filesystem::directory_entry` elements explicitly
for (const fs::directory_entry& dir_entry :
fs::recursive_directory_iterator("sandbox"))
{
std::cout << dir_entry << '\n';
}
std::cout << "-----------------------------\n";
// Iterate over the `std::filesystem::directory_entry` elements using `auto`
for (auto const& dir_entry : fs::recursive_directory_iterator("sandbox"))
{
std::cout << dir_entry << '\n';
}
fs::remove_all("sandbox");
}
Run Code Online (Sandbox Code Playgroud)
编译器返回:
/main.cpp:17:19:错误:二进制表达式的操作数无效('std::__1::ostream'(又名'basic_ostream')和'const fs::directory_entry')std::cout << dir_entry << std::endl;
有人可以帮忙吗?