Googletest: CLANG compiles where GCC fails - c++

Here is a very simple script using gtest (saved in file gt.cpp)
#include<gtest/gtest.h>
double timesTwo(double x){return x*2;}
TEST(testTimesTwo, integerTests){EXPECT_EQ(6, timesTwo(3));}
int main(int argc, char* argv[]){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
The script compiles fine with CLANG (Apple clang version 11.0.3)
clang++ -std=c++17 -lgtest gt.cpp -o gt
but fails with GCC (g++ (GCC) 10.2.0)
g++ -std=c++17 -lgtest gt.cpp -o gt
gt.cpp:2:9: fatal error: gtest/gtest.h: No such file or directory
2 | #include<gtest/gtest.h>
|
Using the -H option in CLANG, I see the header file is included from /usr/local/include. Also, I can find the libgtest.a file in /usr/local/bin/. So, I did
g++ -std=c++17 gt.cpp -o gt -I/usr/local/include/ -L/usr/local/lib/ -lgtest
and got a long list of undefined symbols starting with
Macavity:test remi$ g++ -std=c++17 gt.cpp -o gt -I/usr/local/include/ -L/usr/local/lib/ -lgtest
Undefined symbols for architecture x86_64:
"__ZN7testing8internal9EqFailureEPKcS2_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_b", referenced from:
__ZN7testing8internal18CmpHelperEQFailureIidEENS_15AssertionResultEPKcS4_RKT_RKT0_ in ccrUwUPO.o
"__ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4findEcm", referenced from:
__ZN7testing8internal11SplitStringERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEcPNS1_6vectorIS7_NS5_IS7_EEEE in libgtest.a(gtest-all.cc.o)
__ZN7testing8internalL21FormatDeathTestOutputERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE in libgtest.a(gtest-all.cc.o)
"__ZNKSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7compareEmmPKcm", referenced from:
Can you help me figure out why GCC fails to compile here?

Here is this error message passed through a http://demangler.com/
Undefined symbols for architecture x86_64:
"_testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)", referenced from:
_testing::AssertionResult testing::internal::CmpHelperEQFailure<int, double>(char const*, char const*, int const&, double const&) in ccrUwUPO.o
"_std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::find(char, unsigned long) const", referenced from:
_testing::internal::SplitString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >*) in libgtest.a(gtest-all.cc.o)
_testing::internal::FormatDeathTestOutput(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"_std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::compare(unsigned long, unsigned long, char const*, unsigned long) const", referenced from:
Note that second and third error complains that linker can't find implementation of: std::string::find(char, unsigned long) and std::string::compare(unsigned long, unsigned long, char const*, unsigned long) const.
Those are parts of standard C++ library. Since std::string is template some parts of it are part of library which uses that template, but is some cases it can be part of C++ runtime.
Now I suspect that you compiled gtest with a clang and try use it when building test with gcc (or vice versa). A bit different implementations of std::string on both compilers lead to divergence in available symbols.
So please make sure that gtest and test application are build with same compiler.
Here is some description of binary compatibility between gcc and clang. There are some hints what problem is, but it is not very formal (maybe I will find better document).

Related

Error linking webrtc-native due to undefined reference to methods having std::string

i am trying to build webrtc version 62 , using the following
1.git checkout -b branch62 refs/remotes/branch-heads/62
2.gn gen out_release_62/x64/Debug --args="rtc_include_tests=false rtc_use_h264=false use_rtti=true is_component_build=false enable_iterator_debugging=false enable_nacl=false target_os=\"linux\" target_cpu=\"x64\" is_debug=true"
3.ninja -C out_release_62/x64/Debug
i am linking to libwebrtc.a and libwebrtc_common.a
but wherever i try to use RTC_CHECK_EQ or RTC_DCHECK_LT i get the the following error
undefined reference to `rtc::FatalMessage::FatalMessage(char const*, int, std::string*)'
and
out_release_62/include/webrtc/rtc_base/checks.h:176: undefined reference to `std::string* rtc::MakeCheckOpString<int, int>(int const&, int const&, char const*)'
./src/testwebrtc.o: In function `rtc::CheckLtImpl(int, int, char const*)':
i had no issue with build branch head 60 ,
i tried linking to librtc_base.a and librtc_base_approved.a but no luck.
A sample program that produces this error is
#include <iostream>
#include <string>
#include <webrtc/rtc_base/checks.h>
#include <webrtc/rtc_base/ssladapter.h>
#include <webrtc/rtc_base/logging.h>
#include <webrtc/api/peerconnectioninterface.h>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
std::string type;
std::string sdp;
webrtc::SdpParseError error;
webrtc::CreateSessionDescription(type,sdp,&error);
RTC_DCHECK(5);
RTC_CHECK_EQ(1,6);
RTC_DCHECK_LT(1,4);
return 0;
}
gcc verison 6.3 linker
g++ -L/home/test/webrtc-checkout-ankur/src/out_release_62/x64/Debug -o "testwebrtc" ./src/testwebrtc.o -lwebrtc -lwebrtc_common -lpthread -lm -ldl
this is the error that is returned
./src/testwebrtc.o: In function `main':
/home/test/workspace/testwebrtc/Debug/../src/testwebrtc.cpp:22: undefined reference to `webrtc::CreateSessionDescription(std::string const&, std::string const&, webrtc::SdpParseError*)'
/home/test/workspace/testwebrtc/Debug/../src/testwebrtc.cpp:24: undefined reference to `rtc::FatalMessage::FatalMessage(char const*, int, std::string*)'
/home/test/workspace/testwebrtc/Debug/../src/testwebrtc.cpp:25: undefined reference to `rtc::FatalMessage::FatalMessage(char const*, int, std::string*)'
./src/testwebrtc.o: In function `rtc::CheckEqImpl(int, int, char const*)':
makefile:45: recipe for target 'testwebrtc' failed
/home/test/webrtc-checkout-ankur/src/out_release_62/include/webrtc/rtc_base/checks.h:176: undefined reference to `std::string* rtc::MakeCheckOpString<int, int>(int const&, int const&, char const*)'
./src/testwebrtc.o: In function `rtc::CheckLtImpl(int, int, char const*)':
/home/test/webrtc-checkout-ankur/src/out_release_62/include/webrtc/rtc_base/checks.h:179: undefined reference to `std::string* rtc::MakeCheckOpString<int, int>(int const&, int const&, char const*)'
collect2: error: ld returned 1 exit status
From my observation any function/method that has std::string in it is showing this behaviour.
_GLIBCXX_USE_CXX11_ABI=0 has been set, and -std=c++0x , this build is done on an ubuntu 16.04 system.
running nm on the built library i get
find -iname '.a' -exec nm -A -C {} \; | grep 'T rtc::FatalMessage::FatalMessage('
./Debug/obj/webrtc/rtc_base/librtc_base_approved.a:checks.o:00000000000001a0 T rtc::FatalMessage::FatalMessage(char const*, int)
./Debug/obj/webrtc/rtc_base/librtc_base_approved.a:checks.o:00000000000005e0 T rtc::FatalMessage::FatalMessage(char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
./Debug/obj/webrtc/rtc_base/librtc_base_approved.a:checks.o:00000000000001a0 T rtc::FatalMessage::FatalMessage(char const*, int)
./Debug/obj/webrtc/rtc_base/librtc_base_approved.a:checks.o:00000000000005e0 T rtc::FatalMessage::FatalMessage(char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
./Debug/obj/webrtc/libwebrtc.a:checks.o:00000000000001a0 T rtc::FatalMessage::FatalMessage(char const*, int)
./Debug/obj/webrtc/libwebrtc.a:checks.o:00000000000005e0 T rtc::FatalMessage::FatalMessage(char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
./Debug/obj/webrtc/libwebrtc.a:checks.o:00000000000001a0 T rtc::FatalMessage::FatalMessage(char const*, int)
./Debug/obj/webrtc/libwebrtc.a:checks.o:00000000000005e0 T rtc::FatalMessage::FatalMessage(char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
and
find -iname '.a' -exec nm -A -C {} \; | grep 'CreateSessionDescription('
./Debug/obj/webrtc/pc/libpeerconnection.a:jsepsessiondescription.o:0000000000000000 T webrtc::CreateSessionDescription(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, webrtc::SdpParseError*)
./Debug/obj/webrtc/libwebrtc.a:jsepsessiondescription.o:0000000000000000 T webrtc::CreateSessionDescription(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, webrtc::SdpParseError*)
please help
you have a standard lib mismatch: on one hand you use std::string, on the other hand you use std::__1::basic_string
in other words: on one hand you use libc++ on the other hand you use libstdc++
You need to use clang and pass
-stdlib=libstdc++
to the compiler command line.
__1 signifies that we are using the libc++ llvm-clang library, which is the default for webrtc builds
__1 is the inlined namespace that libc++ uses,
so as to solve this particular issue, we should either use libc++ instead of libstdc++ in our application when we link to libwebrtc,
or
build webrtc to use libstdc++,
to let gn know that we intend to use libstdc++, we pass use_custom_libcxx=false and use_custom_libcxx_for_host=false as args

googletest Undefined symbols for architecture x86_64 error

Let GTEST_DIR be the environment variable storing the path to the googletest directory. (I cloned googletest-master from googletest's github repo.)
I cd'ed into $GTEST_DIR, did a mkdir build && cd build, then executed the following command :
cmake .. -DCMAKE_C_COMPILER=$GNU-6.0.0/bin/gcc-6.0.0 -DCMAKE_CXX_COMPILER=$GNU-6.0.0/bin/g++-6.0.0
where GNU-6.0.0 is the path to my gnu install. This generated a Makefile inside $GTEST_DIR/build that I tweaked as follows : I've added
CC = $GNU-6.0.0/bin/gcc-6.0.0
CXX = $GNU-6.0.0/bin/g++-6.0.0
at its beginning, to be sure that the c and c++ compilers that will be used will be those I want to be used. Then I ran make which produced archive files libgtest.a and libgtest_main.a inside $GTEST_DIR/build.
Next step : in a same folder I put a main test source file main.cpp containg :
#include "path/to/gtest.h"
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
and a dummy test dummy_test.cpp containing :
#include "path/to/gtest.h"
TEST(dummy_test, test1)
{
EXPECT_EQ(1,1);
}
and a Makefile containing :
CC = gcc-6.0.0
CXX = g++-6.0.0
CPPFLAGS += -isystem $(GTEST_DIR)/include
LDFLAGS := -L/usr/lib -lpthread -L$(GTEST_DIR)/build -lgtest
all :
$(CXX) -o cpptests $(CPPFLAGS) ./main.cpp ./dummy_test.cpp $(LDFLAGS)
clean :
rm -rf ./cpptests
Running make I have this output :
Undefined symbols for architecture x86_64:
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::end() const", referenced from:
testing::internal::XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::data() const", referenced from:
testing::internal::PrintStringTo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_ostream<char, std::char_traits<char> >*) in libgtest.a(gtest-all.cc.o)
__gnu_cxx::__enable_if<std::__is_char<char>::__value, bool>::__type std::operator==<char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) const", referenced from:
bool testing::(anonymous namespace)::IsSubstringPred<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find(char, unsigned long) const", referenced from:
testing::internal::SplitString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*) in libgtest.a(gtest-all.cc.o)
testing::internal::FormatDeathTestOutput(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
testing::internal::(anonymous namespace)::SplitEscapedString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
...
...
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
The entire output is in this snippet while the output of nm libgtest.a is in this snippet.
Precision : I am under mac os x 10.10.5. As I want to continue working, developping code and testing it, while the aforementionned error with gcc/g++ 6.0.0 is solved, I tried to switch to another compiler : clang, and I remarked that I had no error at all with it.
Remark libgtest.a was initially (that is, before I asked this question) built "by error" with clang and used in tests with g++-6.1.0 when I encountered the error, that's why I decided to rebuild libgtest.a with g++-6.1.0, thinking that it would solve the problem, but it didn't, which led me to post here.
The problem seems to be due to some linking issues. Have you used the linker flags correctly. For gtest, we need to compile with -lgtest and the linker can link it correctly. Similarly, we need to have the flags for all possible libraries that we are linking against.
The code
TEST(dummy_test, test1) {
EXPECT_EQ(1,1); }
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); }
compiles perfectly for me with command
clang++ -std=c++11 main.cpp -lgtest
and I could run the single test without an issue.
I was getting following error
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
with the command
/usr/bin/g++ -g testgoogletestsetup.cpp -std=c++11 -o testgoogletestsetup -lgtest
Solution:
Library gtest_main needs to be linked (and the library gtest too) to compile it successfully. For that provide the linker flags as -lgtest_main as shown in below command.
/usr/bin/g++ -g testgoogletestsetup.cpp -std=c++11 -o testgoogletestsetup -lgtest -lgtest_main

Clang undefined behavior checker link error

Upon trying to compile a C++ source file with clang 3.5 with the undefined behavior checker
clang++-3.5 -std=c++11 -fsanitize=undefined main.cpp
I am getting the following error upon linking:
Undefined symbols for architecture x86_64:
"typeinfo for __cxxabiv1::__class_type_info", referenced from:
__ubsan::checkDynamicType(void*, void*, unsigned long) in libclang_rt.ubsan_osx.a(ubsan_type_hash.o)
isDerivedFromAtOffset(__cxxabiv1::__class_type_info const*, __cxxabiv1::__class_type_info const*, long) in libclang_rt.ubsan_osx.a(ubsan_type_hash.o)
findBaseAtOffset(__cxxabiv1::__class_type_info const*, long) in libclang_rt.ubsan_osx.a(ubsan_type_hash.o)
"typeinfo for __cxxabiv1::__si_class_type_info", referenced from:
isDerivedFromAtOffset(__cxxabiv1::__class_type_info const*, __cxxabiv1::__class_type_info const*, long) in libclang_rt.ubsan_osx.a(ubsan_type_hash.o)
findBaseAtOffset(__cxxabiv1::__class_type_info const*, long) in libclang_rt.ubsan_osx.a(ubsan_type_hash.o)
"typeinfo for __cxxabiv1::__vmi_class_type_info", referenced from:
isDerivedFromAtOffset(__cxxabiv1::__class_type_info const*, __cxxabiv1::__class_type_info const*, long) in libclang_rt.ubsan_osx.a(ubsan_type_hash.o)
findBaseAtOffset(__cxxabiv1::__class_type_info const*, long) in libclang_rt.ubsan_osx.a(ubsan_type_hash.o)
ld: symbol(s) not found for architecture x86_64
Do I need to link with an additional library?
It seems you are missing libc++abi. Try adding
-lc++abi
to your link command.

Linker error trying to embed v8

I am trying to make a simple program where I embed Google's V8 Javascript engine. The instructions seem straight forward, but I'm getting an odd link error.
I'm on Mac OS X 10.9, and I'm using Xamarin Studio as my IDE which appears to be using g++ as the compiler.
I compiled v8 using the instructions on their site. Specifically, after all the make dependencies, I ran:
make x64.release
This appeared to complete successfully.
Here's the output I'm getting when I try to build my project:
g++ -o "/Users/mike/Projects/vate/vate/bin/Debug/vate" "/Users/mike/Projects/vate/vate/bin/Debug/hello_world.o" -v -L"/Users/mike/Projects/v8/xcodebuild/Debug" -l"icudata" -l"icui18n" -l"v8_snapshot" -l"icuuc" -l"v8_libbase" -l"v8_base" -l"v8_libplatform"
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o /Users/mike/Projects/vate/vate/bin/Debug/vate -L/Users/mike/Projects/v8/xcodebuild/Debug /Users/mike/Projects/vate/vate/bin/Debug/hello_world.o -licudata -licui18n -lv8_snapshot -licuuc -lv8_libbase -lv8_base -lv8_libplatform -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"std::string::c_str() const", referenced from:
v8::internal::Logger::SharedLibraryEvent(std::string const&, unsigned long, unsigned long) in libv8_base.a(log.o)
"std::allocator::allocator()", referenced from:
v8::base::OS::GetSharedLibraryAddresses() in libv8_libbase.a(platform-macos.o)
"std::allocator::~allocator()", referenced from:
v8::base::OS::GetSharedLibraryAddresses() in libv8_libbase.a(platform-macos.o)
"std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&)", referenced from:
v8::base::OS::GetSharedLibraryAddresses() in libv8_libbase.a(platform-macos.o)
"std::basic_string, std::allocator >::basic_string(std::string const&)", referenced from:
v8::base::OS::SharedLibraryAddress::SharedLibraryAddress(v8::base::OS::SharedLibraryAddress const&) in libv8_libbase.a(platform-macos.o)
v8::base::OS::SharedLibraryAddress::SharedLibraryAddress(std::string const&, unsigned long, unsigned long) in libv8_libbase.a(platform-macos.o)
"std::basic_string, std::allocator >::~basic_string()", referenced from:
v8::base::OS::SharedLibraryAddress::~SharedLibraryAddress() in libv8_base.a(log.o)
v8::base::OS::GetSharedLibraryAddresses() in libv8_libbase.a(platform-macos.o)
v8::base::OS::SharedLibraryAddress::~SharedLibraryAddress() in libv8_libbase.a(platform-macos.o)
"std::string::operator=(std::string const&)", referenced from:
v8::base::OS::SharedLibraryAddress::operator=(v8::base::OS::SharedLibraryAddress const&) in libv8_libbase.a(platform-macos.o)
"std::_Rb_tree_decrement(std::_Rb_tree_node_base*)", referenced from:
std::_Rb_tree_iterator >::operator--() in libv8_base.a(allocation-tracker.o)
std::_Rb_tree_iterator >::operator--() in libv8_base.a(lithium-codegen.o)
std::_Rb_tree_iterator::operator--() in libv8_base.a(verifier.o)
std::_Rb_tree_iterator::operator--() in libv8_base.a(instruction.o)
std::_Rb_tree_iterator >::operator--() in libv8_base.a(instruction-selector.o)
std::_Rb_tree_iterator::operator--() in libv8_base.a(typer.o)
std::_Rb_tree_iterator::operator--() in libv8_base.a(graph-visualizer.o)
...
"std::_Rb_tree_increment(std::_Rb_tree_node_base const*)", referenced from:
std::_Rb_tree_const_iterator >::operator++() in libv8_base.a(lithium.o)
std::_Rb_tree_const_iterator::operator++() in libv8_base.a(verifier.o)
std::_Rb_tree_const_iterator >::operator++() in libv8_base.a(instruction.o)
std::_Rb_tree_const_iterator::operator++() in libv8_base.a(typer.o)
std::_Rb_tree_const_iterator::operator++() in libv8_base.a(graph-visualizer.o)
"std::_Rb_tree_increment(std::_Rb_tree_node_base*)", referenced from:
std::_Rb_tree_iterator >::operator++() in libv8_base.a(allocation-tracker.o)
std::_Rb_tree_iterator >::operator++(int) in libv8_base.a(allocation-tracker.o)
"std::__throw_length_error(char const*)", referenced from:
std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, v8::base::OS::SharedLibraryAddress const&) in libv8_libbase.a(platform-macos.o)
std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, v8::internal::compiler::Reducer* const&) in libv8_base.a(pipeline.o)
std::vector >::_M_fill_insert(__gnu_cxx::__normal_iterator > >, unsigned long, v8::internal::compiler::Node* const&) in libv8_base.a(ast-graph-builder.o)
std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, v8::internal::compiler::Node* const&) in libv8_base.a(ast-graph-builder.o)
std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, v8::internal::compiler::FrameStateDescriptor* const&) in libv8_base.a(instruction.o)
std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, v8::internal::compiler::Node* const&) in libv8_base.a(simplified-lowering.o)
std::vector >::reserve(unsigned long) in libv8_base.a(instruction-selector.o)
...
"std::__throw_out_of_range(char const*)", referenced from:
std::vector >::_M_range_check(unsigned long) const in libv8_base.a(graph-reducer.o)
std::vector >::_M_range_check(unsigned long) const in libv8_base.a(ast-graph-builder.o)
std::vector >::_M_range_check(unsigned long) const in libv8_base.a(verifier.o)
std::vector >::_M_range_check(unsigned long) const in libv8_base.a(verifier.o)
std::vector >::_M_range_check(unsigned long) const in libv8_base.a(graph-replay.o)
std::vector >::_M_range_check(unsigned long) const in libv8_base.a(js-context-specialization.o)
std::vector >::_M_range_check(unsigned long) const in libv8_base.a(graph-builder.o)
...
"std::_Rb_tree_rebalance_for_erase(std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)", referenced from:
std::_Rb_tree, std::_Select1st >, std::less, std::allocator > >::erase(std::_Rb_tree_iterator >) in libv8_base.a(allocation-tracker.o)
"std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)", referenced from:
std::_Rb_tree, std::_Select1st >, std::less, std::allocator > >::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::pair const&) in libv8_base.a(allocation-tracker.o)
std::_Rb_tree, v8::internal::Handle, std::_Identity >, std::less >, v8::internal::zone_allocator > >::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, v8::internal::Handle const&) in libv8_base.a(lithium-codegen.o)
std::_Rb_tree, std::less, v8::internal::zone_allocator >::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, v8::internal::compiler::Node* const&) in libv8_base.a(verifier.o)
std::_Rb_tree, std::less, v8::internal::zone_allocator >::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, int const&) in libv8_base.a(instruction.o)
std::_Rb_tree, std::_Select1st >, std::less, v8::internal::zone_allocator > >::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::pair const&) in libv8_base.a(instruction-selector.o)
std::_Rb_tree, std::less, v8::internal::zone_allocator >::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, v8::internal::compiler::Node* const&) in libv8_base.a(typer.o)
std::_Rb_tree, std::less, v8::internal::zone_allocator >::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, v8::internal::compiler::Node* const&) in libv8_base.a(graph-visualizer.o)
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have a feeling that I compiled v8 wrong, or perhaps for a different compiler, but I can't find any way to customize that. Has anyone else seen this?
I have figured it out. I had two problems:
I was confused about which compiler I was actually using. On OSX, g++ is actually symlinked to clang++, so I needed to compile V8 using the Clang conventions, not g++
The instructions on compiling with clang on Google's page are incorrect, per this bug: https://code.google.com/p/v8/issues/detail?id=3072
For reference, to compile with clang, you need to configure your environment like this:
export CXX=`which clang++`
export CC=`which clang`
export CPP="`which clang` -E -std=c++11 -stdlib=libc++"
export LINK="`which clang++` -std=c++11 -stdlib=libc++"
export CXX_host=`which clang++`
export CC_host=`which clang`
export CPP_host="`which clang` -E"
export LINK_host=`which clang++`
export GYP_DEFINES="clang=1 mac_deployment_target=10.9"
You can then run your make whatever command as normal.
Obviously, requires that xcode be installed so that clang and clang++ exist.

Why can't clang with libc++ in c++0x mode link this boost::program_options example?

Compiling this example code for boost::program_options: http://svn.boost.org/svn/boost/trunk/libs/program_options/example/first.cpp
...on MacOS Lion (10.7.2), using boost-1.48.0 installed with MacPorts:
$ clang++ -v
Apple clang version 3.0 (tags/Apple/clang-211.12) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin11.2.0
Thread model: posix
$ clang++ -std=c++0x --stdlib=libc++ -lc++ -I/opt/local/include -L/opt/local/lib -lboost_program_options first.cpp -o first
Undefined symbols for architecture x86_64:
"boost::program_options::options_description::options_description(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int, unsigned int)", referenced from:
_main in cc-6QQcwm.o
"boost::program_options::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, boost::program_options::options_description const&)", referenced from:
_main in cc-6QQcwm.o
"boost::program_options::abstract_variables_map::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
boost::program_options::variables_map::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const in cc-6QQcwm.o
"boost::program_options::detail::cmdline::set_additional_parser(boost::function1<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>)", referenced from:
boost::program_options::basic_command_line_parser<char>::extra_parser(boost::function1<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>) in cc-6QQcwm.o
"boost::program_options::detail::cmdline::cmdline(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)", referenced from:
boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*) in cc-6QQcwm.o
"boost::program_options::to_internal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > boost::program_options::to_internal<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) in cc-6QQcwm.o
"boost::program_options::invalid_option_value::invalid_option_value(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
void boost::program_options::validate<int, char>(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, int*, long) in cc-6QQcwm.o
"boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const& boost::program_options::validators::get_single_string<char>(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, bool) in cc-6QQcwm.o
"boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, bool) const", referenced from:
vtable for boost::program_options::typed_value<int, char> in cc-6QQcwm.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The same code compiled/linked with g++4.7 installed with MacPorts:
$ g++-mp-4.7 -std=c++0x -I/opt/local/include -L/opt/local/lib -lboost_program_options -o first first.cpp
... works fine. As does using clang without libc++:
clang++ -std=c++0x -I/opt/local/include -L/opt/local/lib -lboost_program_options first.cpp -o first
What's wrong? Why does boost::program_options and libc++ not work together?
You need to rebuild boost using clang++ -stdlib=libc++.
libc++ is not binary compatible with gcc's libstdc++ (except for some low level stuff such as operator new). For example the std::string in gcc's libstdc++ is refcounted, whereas in libc++ it uses the "short string optimization". If you were to accidentally mix these two strings in the same program (and mistake them for the same data structure), you would inevitably get a run time crash.
This accident is exactly what has occurred in your case.
In order to turn this run time crash into a link time error, libc++ uses a C++11 language feature called inline namespace to change the ABI of std::string without impacting the API of std::string. That is, to you std::string looks the same. But to the linker, std::string is being mangled as if it is in namespace std::__1. Thus the linker knows that std::basic_string and std::__1::basic_string are two different data structures (the former coming from gcc's libstdc++ and the latter coming from libc++).