undefined reference to std::basic_string

2019-04-12 03:17发布

I'm compiling my project and seeing link problem

$ g++ -Wl,-z,now -Wl,-z,relro -pthread -Wl,-z,noexecstack -fPIC -Wl,--threads -Wl,--thread-count=4 -B../../third_party/gold -L. -Wl,-uIsHeapProfilerRunning,-uProfilerStart -Wl,-u_Z21InitialMallocHook_NewPKvj,-u_Z22InitialMallocHook_MMapPKvS0_jiiix,-u_Z22InitialMallocHook_SbrkPKvi -Wl,-u_Z21InitialMallocHook_NewPKvm,-u_Z22InitialMallocHook_MMapPKvS0_miiil,-u_Z22InitialMallocHook_SbrkPKvl -Wl,-u_ZN15HeapLeakChecker12IgnoreObjectEPKv,-u_ZN15HeapLeakChecker14UnIgnoreObjectEPKv -Wl,--icf=none -Wl,-rpath=\$ORIGIN/lib/ -Wl,-rpath-link=lib/ -o cameo -Wl,--start-group obj/cameo/src/runtime/app/cameo.cameo_main.o obj/media/libmedia_sse.a ...(omitted many *.a here) -Wl,--end-group -lX11 -lXcursor -lXrandr -lXrender -lrt -ldl -lgmodule-2.0 -lgobject-2.0 -lgthread-2.0 -lglib-2.0 -lXtst -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lXi -lXcomposite -lasound -lXdamage -lXext -lXfixes -lnss3 -lnssutil3 -lsmime3 -lplds4 -lplc4 -lnspr4 -lgconf-2 -lresolv -ldbus-1 -lcups -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lgnutls -lgcrypt -lz -lpthread -lm -lcrypt -L/lib/x86_64-linux-gnu -lexpat -ludev
/usr/include/c++/4.7/ext/new_allocator.h:110: error: undefined reference to 'std::basic_string<unsigned short, base::string16_char_traits, std::allocator<unsigned short> >::basic_string(std::basic_string<unsigned short, base::string16_char_traits, std::allocator<unsigned short> >&&)'
/usr/include/c++/4.7/bits/stl_construct.h:77: error: undefined reference to 'std::basic_string<unsigned short, base::string16_char_traits, std::allocator<unsigned short> >::basic_string(std::basic_string<unsigned short, base::string16_char_traits, std::allocator<unsigned short> >&&)'
error: ld returned 1 exit status

The std::basic_string symbol in my gcc library as follows:

$ find /usr -name libstdc++.a
/usr/lib/gcc/x86_64-linux-gnu/4.6/32/libstdc++.a
/usr/lib/gcc/x86_64-linux-gnu/4.6/libstdc++.a
/usr/lib/gcc/x86_64-linux-gnu/4.7/32/libstdc++.a
/usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a

$ nm -C /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a| grep basic_string | grep '&&'
nm: compatibility-debug_list-2.o: no symbols
nm: compatibility-list-2.o: no symbols
nm: compatibility-parallel_list-2.o: no symbols
0000000000000000 W std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::assign(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&&)
0000000000000000 W std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::operator=(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&&)
0000000000000000 W std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::basic_string(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&&)
0000000000000000 W std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::basic_string(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&&)
0000000000000000 n std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::basic_string(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&&)
0000000000000000 W std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string&&)
0000000000000000 W std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string&&)
0000000000000000 n std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string&&)

How can I fix this?

--EDIT--

The problem fixed after I removed -std=gnu++11 from the compile command. It appears that the Chromium base library of string16 is having compatibility issue with C++11.

1条回答
Deceive 欺骗
2楼-- · 2019-04-12 03:54

You're trying to use basic_string<unsigned short, base::string16_char_traits> but that does not come from the standard library, the libstdc++.so library only contains basic_string<char> and basic_string<wchar_t>

That specialization must be used in one of the libraries you're using, so you should find out which and ensure you link to it correctly. If you're compiling with -fno-implicit-templates then you may need an explicit instantiation.

It might help to look for base::string16_char_traits to find where the instantiation comes from. A quick Google suggests it's from a Chromium library, so maybe you're not linking to that, or maybe you need to put that library later in the link command.

查看更多
登录 后发表回答