gpt4 book ai didi

c++ - `std::filesystem::directory_iterator` 编译器问题

转载 作者:行者123 更新时间:2023-11-30 02:18:13 29 4
gpt4 key购买 nike

许多人(例如 12 )询问如何获得 std::filesystem::directory_iterator去工作,但在我读完这些之后我仍然遇到了麻烦。

我正在尝试构建一个小型静态库。在将目录迭代器添加到一些源文件中后,我更新了我的 gcc,并添加了 -lstdc++fs有点,但似乎没有任何效果,因为我不断收到错误消息

fatal error: filesystem: No such file or directory
#include <filesystem>

如果我输入 gcc --version , 我得到

gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

如果我输入 gcc-8 --version我明白了

gcc-8 (Ubuntu 8.1.0-1ubuntu1) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

这是我编译所有内容的小 shell 脚本。我也尝试了一些其他变体。

EIGEN=/usr/include/eigen3

cd ./bin
for file in ../src/*cpp; do
g++ -std=c++11 -fPIC -c -I$EIGEN -I../include -O3 $file "-lstdc++fs"
done
ar crv libfoo.a *.o
cd ..

最佳答案

<filesystem>仅随 C++17 添加到 C++ 标准库。

g++ 7.3 (您的默认 g++ )不完全符合此分数。它不会找到 <filesystem>-std=c++17 .按理说不会定位<filesystem>-std=c++11 ,您发布的脚本要求它这样做。但它会定位<experimental/filesystem>std=c++11或以后。

你还有g++-8 (大概是 g++ 8.1/8.2)。它将找到 <filesystem>std=c++17 :

$ cat main.cpp 
#include <filesystem>

int main()
{
return 0;
}
$ g++-8 -std=c++17 main.cpp && echo $?
0

有趣的是,它也会对 std=c++11 执行此操作或 std=c++14 :

$ g++-8 -std=c++11 main.cpp && echo $?
0
$ g++-8 -std=c++14 main.cpp && echo $?
0

g++-8您不需要链接过渡库 libstdc++fs .

(顺便说一句,聪明的钱总是在编译时启用严格的警告: ... -Wall -Wextra ... )

关于c++ - `std::filesystem::directory_iterator` 编译器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52472023/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com