title: Locating iostream in Clang++: fatal error: 'iostream' file not found
body: <p>I wrote the following simple C++ program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main() {
      cout &lt;&lt; "Hello, World" &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>When I compile this with g++, it works perfectly. When I try to compile with Clang++, I get the following error:</p>
  
  <pre><code>main.cpp:1:10: fatal error: 'iostream' file not found
  #include &lt;iostream&gt;
           ^~~~~~~~~~
  1 error generated.
  </code></pre>
  
  <p>Running with the <code>-v</code> parameter, I see the following:</p>
  
  <pre><code>ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/x86_64-linux-gnu"
  ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/backward"
  ignoring nonexistent directory "/include"
  ignoring duplicate directory "/usr/include/clang/6.0.0/include"
  #include "..." search starts here:
  #include &lt;...&gt; search starts here:
   /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++
   /usr/include/clang/6.0.0/include
   /usr/local/include
   /usr/include/x86_64-linux-gnu
   /usr/include
  End of search list.
  </code></pre>
  
  <p>Looking into these folders individually, I found that in <code>/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++</code> (or, more concisely, in <code>/usr/include/c++</code>) I have the following directories:</p>
  
  <pre><code>drwxr-xr-x   5 root root 4.0K Feb  4 09:38 .
  drwxr-xr-x 101 root root  20K Feb  4 12:22 ..
  drwxr-xr-x  12 root root  12K May 24  2018 5
  drwxr-xr-x  12 root root  12K Oct  9 14:53 7
  drwxr-xr-x   5 root root 4.0K Feb  4 09:38 v1
  lrwxrwxrwx   1 root root    1 Apr 11  2018 5.5.0 -&gt; 5
  lrwxrwxrwx   1 root root    1 Apr 15  2018 7.3.0 -&gt; 7
  </code></pre>
  
  <p>Within each of the <code>5</code>, <code>7</code>, and <code>v1</code> directories there exists a file called <code>iostream</code></p>
  
  <p>Also in <code>/usr/include/x86_64-linux-gnu</code> there exists a <code>c++</code> directory which looks exactly like this one (with <code>5</code>, <code>7</code>, <code>5.5.0</code>, and <code>7.3.0</code> directories).</p>
  
  <p>Also in <code>/usr/include</code> there exists a <code>c++</code> directory which looks exactly like the two above</p>
  
  <p>I'm not sure how my dev environment became such a mess, but at this point I would just like to know how to fix it so that Clang++ will successfully find one of these 9 instances of <code>iostream</code> instead of throwing an error that it doesn't exist. Do I need to add an environment variable to tell Clang where to look? Do I need to pass a command-line parameter to tell Clang to search recursively?</p>
  
  <h2>Update (1)</h2>
  
  <p>When I try building with <code>libc++</code> I get the following error:</p>
  
  <pre><code>$&gt; clang++ -stdlib=libc++ main.cpp
  /usr/bin/ld: cannot find -lc++abi
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>When I try building with the include path manually overridden, I get the following error:</p>
  
  <pre><code>$&gt; clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 main.cpp
  /usr/bin/ld: cannot find -lstdc++
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>When I try both, I get the following (incredibly large) error:</p>
  
  <pre><code>$&gt; clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 -stdlib=libc++ main.cpp
  In file included from main.cpp:1:
  In file included from /usr/include/c++/7/iostream:39:
  In file included from /usr/include/c++/7/ostream:38:
  In file included from /usr/include/c++/7/ios:42:
  In file included from /usr/include/c++/7/bits/ios_base.h:41:
  In file included from /usr/include/c++/7/bits/locale_classes.h:40:
  In file included from /usr/include/c++/7/string:52:
  In file included from /usr/include/c++/7/bits/basic_string.h:6352:
  In file included from /usr/include/c++/7/ext/string_conversions.h:41:
  In file included from /usr/include/c++/7/cstdlib:77:
  /usr/include/c++/7/bits/std_abs.h:56:3: error: declaration conflicts with target of using declaration already in scope
    abs(long __i) { return __builtin_labs(__i); }
    ^
  /usr/include/c++/v1/stdlib.h:111:44: note: target of using declaration
  inline _LIBCPP_INLINE_VISIBILITY long      abs(     long __x) _NOEXCEPT {return  labs(__x);}
                                             ^
  /usr/include/c++/7/bits/std_abs.h:52:11: note: using declaration
    using ::abs;
            ^
  /usr/include/c++/7/bits/std_abs.h:61:3: error: declaration conflicts with target of using declaration already in scope
    abs(long long __x) { return __builtin_llabs (__x); }
    ^
  /usr/include/c++/v1/stdlib.h:113:44: note: target of using declaration
  inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {return llabs(__x);}
                                             ^
  /usr/include/c++/7/bits/std_abs.h:52:11: note: using declaration
    using ::abs;
            ^
  In file included from main.cpp:1:
  In file included from /usr/include/c++/7/iostream:39:
  In file included from /usr/include/c++/7/ostream:38:
  In file included from /usr/include/c++/7/ios:42:
  In file included from /usr/include/c++/7/bits/ios_base.h:41:
  In file included from /usr/include/c++/7/bits/locale_classes.h:40:
  In file included from /usr/include/c++/7/string:52:
  In file included from /usr/include/c++/7/bits/basic_string.h:6352:
  In file included from /usr/include/c++/7/ext/string_conversions.h:41:
  /usr/include/c++/7/cstdlib:177:3: error: declaration conflicts with target of using declaration already in scope
    div(long __i, long __j) { return ldiv(__i, __j); }
    ^
  /usr/include/c++/v1/stdlib.h:116:42: note: target of using declaration
  inline _LIBCPP_INLINE_VISIBILITY  ldiv_t div(     long __x,      long __y) _NOEXCEPT {return  ldiv(__x, __y);}
                                           ^
  /usr/include/c++/7/cstdlib:145:11: note: using declaration
    using ::div;
            ^
  </code></pre>
  
  <p>As a reminder, <strong>I'm literally just trying to compile Hello, World</strong></p>
  
  <p>I also tried uninstalling and re-installing Clang with the following command:</p>
  
  <pre><code>$&gt; sudo apt-get purge --auto-remove clang
  $&gt; sudo apt-get update
  $&gt; sudo apt-get install clang
  </code></pre>
  
  <p>This had no effect. I'm running Ubuntu 18.04 and I have no idea what's wrong or where to start with fixing it. My build environment is in shambles.</p>
  
  <p>If possible I would like to get Clang working instead of falling back to using G++, because my IDE seems to be automatically detecting Clang and using it for syntax checking. This means that every C++ program I've written has one fatal error on line one ("iostream not found") and the rest of the file goes unchecked because that first one is a fatal error.</p>
  
  <h2>Update (2)</h2>
  
  <p>I've tried installing a few more packages from the Ubuntu apt repository with no luck:</p>
  
  <pre><code>$&gt; sudo apt-get install libc++1 libc++1-9 libc++abi1 libc++abi1-9 llvm-9 llvm-9-dev
  $&gt; clang++ -isystem /usr/include/c++/7 -isystem /usr/include/x86_64-linux-gnu/c++/7 -stdlib=libc++ main.cpp
  /usr/bin/ld: cannot find -lc++
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>I also tried <code>sudo apt-get install lc++1</code> only to find this is an <strong><em>entirely unrelated</em></strong> package.</p>
  
  <h2>Update (3)</h2>
  
  <p>I spent several more hours trying to resolve this, installing multiple packages both from apt and from source, trying different versions of various tools, manually copying in libraries from other sources, and even hopped onto the Clang IRC and spoke to several very knowledgeable developers directly.</p>
  
  <p>No one was able to figure out what's wrong with my laptop, and nothing I did ever got it working.</p>
  
  <p>Unfortunately I won't still have this laptop in another two weeks, so I'll likely need to close this issue as "cannot reproduce" - because once the laptop is gone I will have no way of replicating the broken development environment.</p>