Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default to lib=stdlibc++ for clang++ on Mac OS Mavericks with Xcode?

Tags:

c++

macos

clang++

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?

like image 308
Joe Malin Avatar asked May 14 '14 23:05

Joe Malin


People also ask

Does Mac use Clang or GCC?

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.

Does macOS have LLVM?

All of Apple's operating systems, iOS, macOS, tvOS and watchOS, are built with LLVM technologies.

Is clang built in to Mac?

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.


2 Answers

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++.

like image 118
Thomas Avatar answered Sep 30 '22 15:09

Thomas


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++");
  • Further Reading
like image 28
l'L'l Avatar answered Sep 30 '22 14:09

l'L'l