我正在尝试使用文件系统.我有-std=c++11 -std=c++1y我的CMakeLists.txt.GCC版本是4.9.2.但是,我有一个错误:
/home/loom/MyProject/src/main.cpp:5:35: fatal error: experimental/filesystem: No such file or directory
#include <experimental/filesystem>
^
compilation terminated.
Run Code Online (Sandbox Code Playgroud)
什么是正确的使用方式std::experimental::filesystem?
我遇到一个非常奇怪的行为,我将其提炼为一个非常基本的测试:
#include <string>
#include <filesystem>
int main(void)
{
const std::string name = "foo";
const std::filesystem::path lock_dir = "/tmp";
std::filesystem::path lockfile = lock_dir / name;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用编译g++ -std=c++17 -Wall -Wextra -Werror -g foo.cpp -o foo。当我运行它时,在添加两个路径的那一行上,我得到了一个std :: bad_alloc异常。这是我在gdb中看到的
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff742c801 in __GI_abort () at abort.c:79
#2 0x00007ffff7a8e1f2 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff7a99e36 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff7a99e81 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7a9a0b5 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 …Run Code Online (Sandbox Code Playgroud) 在尝试使用当前的GCC在C++中使用一些新的/实验性的东西时,我正面临链接错误.似乎已经发布了类似的问题,但仍然出现错误.
有人可以解释一下我在这里做了什么/错在哪里?希望我已经提供了与此相关的所有详细信息.
TEST.CPP
#include<iostream>
#include<string>
#include<experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, const char* argv[])
{
std::string s(argv[0]);
fs::path p(s);
std::cout << "p = " << p << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经构建(调试版本)新gcc版本6.0.0 20151122(实验)(GCC)为g ++ - 6.0.0.这里我使用FS-TS所需的新-lstdc ++ fs选项.我可以使用其他新东西,如C++概念.
~/practice/gcc6$ g++-6.0.0 -v -g -lstdc++fs -o out test.cpp
Using built-in specs.
COLLECT_GCC=g++-6.0.0
COLLECT_LTO_WRAPPER=/usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_6_0 --with-gmp=/usr/gcc_6_0 --with-mpfr=/usr/gcc_6_0 --with-mpc=/usr/gcc_6_0 --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-6.0.0
Thread model: posix
gcc version 6.0.0 …Run Code Online (Sandbox Code Playgroud) 我正在使用这段代码
long filesize (const char * filename)
{
ifstream file (filename, ios::in|ios::binary);
file.seekg (0, ios::end);
return file.tellg();
}
Run Code Online (Sandbox Code Playgroud)
以字节为单位返回文件大小.但是我没有读取权限的文件导致返回-1.有没有办法使用c ++或c来返回文件和目录的大小,即使在没有读取权限的情况下也能正常工作?我正在寻找一段时间,但未能找到可靠的解决方案.
关于experimental/filesystem在最新版本的GCC和Clang中进行编译有几个问题:experimental :: filesystem链接器错误
但是现在filesystem已经被c ++ 17接受了所以不再需要experimental或者是-lstdc++fs旗帜吧?
错了我甚#include <filesystem>至不能在头版上clang++或者g++我尝试时:http://melpon.org/wandbox
我还需要其他一些论点吗?-lstdc++fs只是给我experimental版本,我怎么能包含官方认可的版本?
我正在尝试使用代码运行程序在visual studio代码中使用experimental :: filesystem编译一个项目,但是我甚至无法在终端中编译它.
代码如下,来自docs的一个非常简单的测试用法:
#include <iostream>
#include<experimental/filesystem>
using namespace std;
namespace fs = std::experimental::filesystem;
int main(){
fs::create_directories("sandbox/a/b");
cout << "done.";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用.编译
g++ -std=c++17 $fullFileName && ./a.out -lstdc++fs
Run Code Online (Sandbox Code Playgroud)
在代码运行器配置或只是
g++ -std=c++17 test.cpp -o test -lstdc++fs
Run Code Online (Sandbox Code Playgroud)
在终端中不编译或一般不工作.
它提供的错误是:
/tmp/cco0g7Qf.o: In function `main':
test.cpp:(.text+0x24): undefined reference to `std::experimental::filesystem::v1::create_directories(std::experimental::filesystem::v1::__cxx11::path const&)'
/tmp/cco0g7Qf.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<char [12], std::experimental::filesystem::v1::__cxx11::path>(char const (&) [12])':
test.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx114pathC2IA12_cS3_EERKT_[_ZNSt12experimental10filesystem2v17__cxx114pathC5IA12_cS3_EERKT_]+0x64): undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
任何帮助都是apreciated,我正在运行linux并且已经检查以确保我的libstdc ++与我的gcc一起是最新的.
当我尝试将其std::filesystem::path用作函数参数时,它在我的计算机上出现段错误。这是一个最小的示例:
#include <filesystem>
void thing(const std::filesystem::path& p) {
return;
}
int main() {
thing("test");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码段导致来自gdb的以下回溯:
#0 0x0000563a5a3814b3 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (this=0x23, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/stl_vector.h:567
#1 0x0000563a5a38132c in std::filesystem::__cxx11::path::~path (this=0x3, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/fs_path.h:208
#2 0x0000563a5a381f74 in std::filesystem::__cxx11::path::_Cmpt::~_Cmpt (this=0x3, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/fs_path.h:643
#3 0x0000563a5a381f8f in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt> (__pointer=0x3) at /usr/include/c++/8/bits/stl_construct.h:98
#4 0x0000563a5a381e3f in std::_Destroy_aux<false>::__destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0) at /usr/include/c++/8/bits/stl_construct.h:108
#5 0x0000563a5a381ab0 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0) at /usr/include/c++/8/bits/stl_construct.h:137
#6 0x0000563a5a3817c1 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*, std::filesystem::__cxx11::path::_Cmpt> (__first=0x3, __last=0x0) at …Run Code Online (Sandbox Code Playgroud) 以下程序崩溃:
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::path p1 = "/usr/lib/sendmail.cf";
std::cout << "p1 = " << p1 << '\n';
}
Run Code Online (Sandbox Code Playgroud)
汇编:
$ g++ -std=c++17 pathExistsTest.cpp
$ ./a.out
p1 = "/usr/lib/sendmail.cf"
[1] 35688 segmentation fault (core dumped) ./a.out
Run Code Online (Sandbox Code Playgroud)
在 Ubuntu 20.04 上测试,编译器为 GCC 8.4.0。
Valgrind,这是切割输出:
==30078== by 0x4AE5034: QAbstractButton::mouseReleaseEvent(QMouseEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.12.8)
==30078== by 0x4A312B5: QWidget::event(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.12.8)
==30078== Address 0x2b is not stack'd, malloc'd or (recently) free'd
==30078==
==30078==
==30078== Process terminating with default action …Run Code Online (Sandbox Code Playgroud) 我要将目录(tmp1)的内容复制到另一个目录(tmp2)。tmp1可能包含文件和其他目录。我想使用C / C ++复制tmp1的内容(包括模式)。如果tmp1包含目录树,则要递归复制它们。
什么是最简单的解决方案?
我找到了一种打开目录并读取每个条目并使用cp命令将其复制的解决方案。还有更简单的解决方案吗?
我试图推出新的filesystemSTL库,但由于某种原因我遇到了错误.该Clang++7网站表明它应该支持新的filesystem图书馆 - 事实上我clang已经超前了g++.
我使用了另一个Stack Exchange帖子中的一些代码,因此根据upvotes的数量它应该是有效的.这可能会转到指定的目录并打印该目录中的所有文件.这是代码.
#include <iostream>
#include <string>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, char *argv[])
{
std::string path = "/home/.../Downloads";
for (const auto & entry : fs::directory_iterator(path))
{
std::cout << entry.path() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误消息是:
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `main':
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator*() const'
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator++()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `path<std::__cxx11::basic_string<char>, std::experimental::filesystem::v1::__cxx11::path>':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_path.h:198: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `directory_iterator': …Run Code Online (Sandbox Code Playgroud) 我使用的是安装了gcc 4.8.5 20150623的CentOS Linux 7.3.1611版.我正在寻找一种方法来安装更新版本的gcc,特别是8.1.我找到了以下网站,介绍如何安装gcc v7 link 1,其中介绍了如何安装gcc v7(使用devtoolset-7-gcc*).我确实遵循了指南,并能够安装gcc v7.虽然,如果我遵循相同的程序,但使用"devtoolset-8-gcc*"而不是返回消息指出"没有包devtoolset-8-gcc*可用.".
执行搜索并找到一个名为"Information for build devtoolset-8-gcc-8.1.1-4.el6" 链接2的页面,但不知道如何安装它.我相信我需要下载rpm源代码,编译/构建源代码,然后最终安装它.
基本上,我正在寻找一种简单的方法来安装gcc v8,就像使用链接1中描述的方法一样.
提前致谢.