添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am writing a personal project in c++ which needs to access to files in some directories, hence I decided to use the filesystem library. I encountered some problems when I try to compile my project on MacOS and on Linux.

The code snippet is the following

#include <iostream>
#include <fstream>
int main(){
    std::string path = "Inner";
    std::cout << "Files in " << path << " directory :" << std::endl;
    for (const auto & entry : std::filesystem::directory_iterator(path))
        std::cout << entry.path() << std::endl;
    return 0;

When I compile it on my MacBook Pro (clang version 11.0.3 (clang-1103.0.32.62)) with

g++ -o test test.cpp -std=c++17 -Wall

everything works fine. But as soon as I move to Linux (Ubuntu 19.04, g++ 8.3.0) I get the following error:

test.cpp: In function ‘int main()’:
test.cpp:8:33: error: ‘std::filesystem’ has not been declared
  for (const auto & entry : std::filesystem::directory_iterator(path)){

I include then the filesystem library with #include <filesystem>:

#include <iostream>
#include <fstream>
#include <filesystem>
int main(){
    std::string path = "Inner";
    std::cout << "Files in " << path << " directory :" << std::endl;
    for (const auto & entry : std::filesystem::directory_iterator(path))
        std::cout << entry.path() << std::endl;
    return 0;

compile it via g++ -o test test.cpp -std=c++17 -Wall -lstdc++fs and everything works fine on Linux too (note that I had to add -lstdc++fs).

Why is there this different behaviour on MacOS and on Linux? Does it depends on the compiler? What happens with Windows OS (I do not have any Windows PC at home)?

I found a related question and its answer here, but it does not seem to explain why in the first case (with clang) everything works fine also without including filesystem library.

The C++ standard allows the standard header files to include declarations from other standard header files. I imagine that in the first case the <fstream> header is also including some or all of the filesystem declarations. – john Sep 22, 2020 at 12:40 I know there's good duplicates for this issue, but can't seem to find them. Anyway, short solution: Always explicitly include the header files needed for the functionality you use. A good reference helps figuring out what headers are needed. – Some programmer dude Sep 22, 2020 at 12:44 @john I thought that with the -std=c++17 I was setting the compile to use the same standard, but clearly I am wrong. – Eddymage Sep 22, 2020 at 12:47 @Eddymage You are using the same standard. It's just that the standard has some flexibility and different compilers make different choices. – john Sep 22, 2020 at 12:48 @Someprogrammerdude Yes, you're right, I always check this but in the filesystem library page I was not able to find any clue about my problem, nor in the notes at bottom page. – Eddymage Sep 22, 2020 at 12:50
  • Using 'g++' is not using clang you should use 'clang++'

  • Gcc should not be platform dependent but it might be different version

  • At any case, you should explicitly include header files needed, and std::filesystem is defined in "<filesystem>"

  • regarding the need to add "lstdc++fs' - this is a hint that actually g++ version is different and uses different llvm versions. As described in https://en.cppreference.com/w/cpp/filesystem

    Notes: Using this library may require additional compiler/linker options. GNU implementation prior to 9.1 requires linking with -lstdc++fs and LLVM implementation prior to LLVM 9.0 requires linking with -lc++fs

    1) What you mean? In macOS when I type g++ it calls its default compiler, that is clang 2) Yeah... so? In one case I am using g++, in the other clang 3) That's the point: why not including the header file in the 1st case it works anyway? But your sentence is incomplete – Eddymage Sep 22, 2020 at 13:12 1. That might be true 2. If you use a different version of GCC/CLANG there is different behavior like I mention in (4), 3. Sometimes different compiler and compilers version act a bit differently in regards to includes - at any case you should include header explicitly. You are welcome! – Yitshak Yarom Sep 22, 2020 at 13:24 1. I checked: it is so; 2) Yes, it was my 1st thought: but I would like to know why is it working this way. The known notes of the cpp reference are not helping in my case 3) How can I know when I have to include the library, if no compile error is is thrown in macOS, for example? I mean, without trying all compilers... This is what my question is about. – Eddymage Sep 23, 2020 at 14:15

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •