Taylor
Taylor

Reputation: 2085

`std::filesystem::directory_iterator` compiler issue

Many people (e.g 1, 2) have asked about how to get std::filesystem::directory_iterator to work, but I'm still having trouble after I've read those.

I am trying to build a small static library. After adding in the directory iterator into some source files, I updated my gcc, and added the -lstdc++fs bit, but nothing seems to work because I keep getting the error message

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

If I type gcc --version, I get

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.

And if I type gcc-8 --version I get

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.

Here's my little shell script that compiles everything. I've tried a few other variations, too.

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 ..

Upvotes: 1

Views: 3806

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61630

<filesystem> was added to the C++ standard library only with C++17.

g++ 7.3 (your default g++) is not fully compliant on this score. It will not locate <filesystem> with -std=c++17. Reasonably, it will not locate <filesystem> with -std=c++11, which your posted script is asking it to. But it will locate <experimental/filesystem> with std=c++11 or later.

You also have g++-8 (presumably g++ 8.1/8.2). It will locate <filesystem> with std=c++17:

$ cat main.cpp 
#include <filesystem>

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

And funnily enough, it will also do so with either std=c++11 or std=c++14:

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

With g++-8 you won't need to link the transitional library libstdc++fs.

(Incidentally, the smart money always enables rigorous warnings in compilation: ... -Wall -Wextra ...)

Upvotes: 3

Related Questions