title: C++ 11 code compiles with `clang++`, but not with `clang -x c++`
body: <h2>Basic Problem</h2>
  
  <p>I have the following code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdint&gt;
  using namespace std;
  int main ()
  {
    int32_t spam;
    spam=5;
    cout &lt;&lt; "Hello World! We like " &lt;&lt; spam &lt;&lt; endl;
    return 0;
  }
  </code></pre>
  
  <p>This compiles nicely when I do <code>clang++ -stdlib=libc++ cpptest.cpp</code>. However, the otherwise excellent SublimeClang parser for Sublime Text 2 doesn't seem to understand it. I figured that might be because it uses <code>clang -x c++</code> instead of <code>clang++</code>, and tried to compile my snippet above using <code>clang -x c++ -std=c++11 -stdlib=libc++ cpptest.cpp</code> (or various permutations of that) instead, but that fails horribly, complaining about the linker.</p>
  
  <p>So my question is, how does <code>clang++</code> differ from <code>clang -x c++</code>? Is it possible to get the above snippet to compile using <code>clang -x c++</code> instead of <code>clang++</code>?</p>
  
  <p>My machine is a 2012 MacBook Air running Mountain Lion.</p>
  
  <h2>Debugging stuff</h2>
  
  <p>Now back at work, I've played a bit more with this, and compared the verbose output of the different versions.</p>
  
  <h3>Calling <code>clang -x c++</code></h3>
  
  <p>When I run <code>clang -v -x c++ -stdlib=libc++ cpptest.cpp</code> I get</p>
  
  <pre><code>Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
  Target: x86_64-apple-darwin12.2.1
  Thread model: posix
   "/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name cpptest.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/clang-module-cache -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/me/Desktop -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-1eOyZn.o -x c++ cpptest.cpp
  clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.1
  ignoring nonexistent directory "/usr/include/c++/v1"
  #include "..." search starts here:
  #include &lt;...&gt; search starts here:
   /usr/bin/../lib/c++/v1
   /usr/local/include
   /usr/bin/../lib/clang/4.1/include
   /usr/include
   /System/Library/Frameworks (framework directory)
   /Library/Frameworks (framework directory)
  End of search list.
   "/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-1eOyZn.o -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a
  Undefined symbols for architecture x86_64:
  [snip]
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <h3>Calling <code>clang++</code></h3>
  
  <p>When I call <code>clang++ -v -stdlib=libc++ cpptest.cpp</code> I get the following.</p>
  
  <pre><code>clang++ -v -stdlib=libc++ cpptest.cpp 
  Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
  Target: x86_64-apple-darwin12.2.1
  Thread model: posix
   "/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name cpptest.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/clang-module-cache -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/me/Desktop -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-E7FTDV.o -x c++ cpptest.cpp
  clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.1
  ignoring nonexistent directory "/usr/include/c++/v1"
  #include "..." search starts here:
  #include &lt;...&gt; search starts here:
   /usr/bin/../lib/c++/v1
   /usr/local/include
   /usr/bin/../lib/clang/4.1/include
   /usr/include
   /System/Library/Frameworks (framework directory)
   /Library/Frameworks (framework directory)
  End of search list.
   "/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-E7FTDV.o -lc++ -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a
  </code></pre>
  
  <h3>Differences and solution?</h3>
  
  <p>The only major difference I can find is that when I run clang with the <code>-x c++</code> flag the <code>-lc++</code> flag does not get added at the end of the line that starts with <code>"/usr/bin/lb"</code>. When I instead compile with <code>clang -v -lc++ -x c++ -stdlib=libc++ cpptest.cpp</code>, everything works. I still don't understand how exactly the <code>clang++</code> and <code>clang -x c++</code> commands are supposed to differ, but this seems to make my example program compile at least, and seems to indicate that the commands do different things with the linker.</p>
  
  <h2>Solution to my Sublime Text 2 problem</h2>
  
  <p>This whole little bug hunt was started with me trying to figure out how to get Sublime Text 2 to stop complaining about code I have that actually works, and now I think I've got it. What was needed was simply to follow the instructions <a href="https://github.com/quarnster/SublimeClang/issues/107" rel="nofollow">here</a> and set the includes to be</p>
  
  <pre><code>/usr/lib/c++/v1
  /usr/include/c++/4.2.1
  /usr/include/c++/4.2.1/ext
  </code></pre>
  
  <p>in the <code>SublimeClang.sublime-settings</code> file.</p>