Basic Problem
I have the following code
#include <iostream>
#include <cstdint>
using namespace std;
int main ()
{
int32_t spam;
spam=5;
cout << "Hello World! We like " << spam << endl;
return 0;
}
This compiles nicely when I do clang++ -stdlib=libc++ cpptest.cpp
. However, the otherwise excellent SublimeClang parser for Sublime Text 2 doesn't seem to understand it. I figured that might be because it uses clang -x c++
instead of clang++
, and tried to compile my snippet above using clang -x c++ -std=c++11 -stdlib=libc++ cpptest.cpp
(or various permutations of that) instead, but that fails horribly, complaining about the linker.
So my question is, how does clang++
differ from clang -x c++
? Is it possible to get the above snippet to compile using clang -x c++
instead of clang++
?
My machine is a 2012 MacBook Air running Mountain Lion.
Debugging stuff
Now back at work, I've played a bit more with this, and compared the verbose output of the different versions.
Calling clang -x c++
When I run clang -v -x c++ -stdlib=libc++ cpptest.cpp
I get
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 <...> 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)
Calling clang++
When I call clang++ -v -stdlib=libc++ cpptest.cpp
I get the following.
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 <...> 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
Differences and solution?
The only major difference I can find is that when I run clang with the -x c++
flag the -lc++
flag does not get added at the end of the line that starts with "/usr/bin/lb"
. When I instead compile with clang -v -lc++ -x c++ -stdlib=libc++ cpptest.cpp
, everything works. I still don't understand how exactly the clang++
and clang -x c++
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.
Solution to my Sublime Text 2 problem
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 here and set the includes to be
/usr/lib/c++/v1
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/ext
in the SublimeClang.sublime-settings
file.