I'm running MacOS X Mavericks with Xcode 5.1.1 including the command line tools. I'm compiling simple C++ programs using clang++ supplied with Xcode, the version info is: Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
What I find is that if I try to run the following command
clang++ -o hello.out hello.cpp
I get the following errors:
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in hello-2ad0da.o
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in hello-2ad0da.o
"std::cout", referenced from:
_main in hello-2ad0da.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<<<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in hello-2ad0da.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I change the command to
clang++ -o hello.out -stdlib=libstdc++ hello.cpp
I don't get any errors.
Is there a way to make "-stdlib=libstdc++" the default for clang++, either with some configuration setting or some environment variable? Also, just for my information, why do I get the error?
Clang is also provided in all major BSD or GNU/Linux distributions as part of their respective packaging systems. From Xcode 4.2, Clang is the default compiler for Mac OS X.
All of Apple's operating systems, iOS, macOS, tvOS and watchOS, are built with LLVM technologies.
Clang and Xcode are not distributed in macOS distributions but are available as downloads from Apple. Xcode command-line developer tools can be downloaded, including Clang but without the full Xcode installation, by typing xcode-select -install in a Terminal window.
MACOSX_DEPLOYMENT_TARGET
may be what you are looking for.
export MACOSX_DEPLOYMENT_TARGET=10.8
should make clang
to default to libstdc++
instead of libc++
.
Before OS X 10.9.x:
The default was
libstdc++
(using clang++ -o hello.out hello.cpp) would have worked fine.
OS X 10.9.x:
The default is
libc++
(as you know the including flag -stdlib=libstdc++ links your project correctly).
Since you're trying to compile code that uses symbols that are not within the design of the newer LLVM libc++
standard library you receive errors. As for changing the default that clang
uses you'd likely have to patch it's ToolChains.cpp (docs) with something such as:
- DAL->AddJoinedArg(0, Opts.getOption(options::OPT_stdlib_EQ), "libc++");
+ DAL->AddJoinedArg(0, Opts.getOption(options::OPT_stdlib_EQ), "libstdc++");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With