`std :: filesystem :: directory_iterator`编译器问题

问题描述 投票:1回答:1

许多人(例如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 ..
c++ c++11 gcc c++17 std-filesystem
1个回答
3
投票

<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++11std=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 ...

© www.soinside.com 2019 - 2024. All rights reserved.