title: Compiler error with filesystem library: clang and g++
body: <p>I am writing a personal project in c++ which needs to access to files in some directories, hence I decided to use the <code>filesystem</code> library. I encountered some problems when I try to compile my project on MacOS and on Linux.</p>
  <p>The code snippet is the following</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  int main(){
  
      std::string path = &quot;Inner&quot;;
  
      std::cout &lt;&lt; &quot;Files in &quot; &lt;&lt; path &lt;&lt; &quot; directory :&quot; &lt;&lt; std::endl;
  
      for (const auto &amp; entry : std::filesystem::directory_iterator(path))
          std::cout &lt;&lt; entry.path() &lt;&lt; std::endl;
  
  
      return 0;
  
  }
  </code></pre>
  <p>When I compile it on my MacBook Pro (clang version 11.0.3 (clang-1103.0.32.62)) with</p>
  <p><code>g++ -o test test.cpp -std=c++17 -Wall</code></p>
  <p>everything works fine. But as soon as I move to Linux (Ubuntu 19.04, g++ 8.3.0) I get the following error:</p>
  <pre><code>test.cpp: In function ‘int main()’:
  test.cpp:8:33: error: ‘std::filesystem’ has not been declared
    for (const auto &amp; entry : std::filesystem::directory_iterator(path)){
  </code></pre>
  <p>I include then the filesystem library with <code>#include &lt;filesystem&gt;</code>:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;filesystem&gt;
  
  int main(){
  
      std::string path = &quot;Inner&quot;;
  
      std::cout &lt;&lt; &quot;Files in &quot; &lt;&lt; path &lt;&lt; &quot; directory :&quot; &lt;&lt; std::endl;
  
      for (const auto &amp; entry : std::filesystem::directory_iterator(path))
          std::cout &lt;&lt; entry.path() &lt;&lt; std::endl;
  
      return 0;
  
  }
  </code></pre>
  <p>compile it via <code>g++ -o test test.cpp -std=c++17 -Wall -lstdc++fs</code> and everything works fine on Linux too (note that I had to add <code>-lstdc++fs</code>).</p>
  <p>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)?</p>
  <hr />
  <p>I found a related question and its answer <a href="https://stackoverflow.com/a/55474923/13156261">here</a>, but it does not seem to explain why in the first case (with clang) everything works fine also without including <code>filesystem</code> library.</p>