C++ 对 std::basic_string 的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17482596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:14:11  来源:igfitidea点击:

undefined reference to std::basic_string

c++gccc++11linkerg++

提问by Deqing

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_stringsymbol in my gcc library as follows:

std::basic_string我的 gcc 库中的符号如下:

$ 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++11from the compile command. It appears that the Chromium base library of string16 is having compatibility issue with C++11.

-std=gnu++11从编译命令中删除后问题解决了。看来 string16 的 Chromium 基础库与 C++11 存在兼容性问题。

回答by Jonathan Wakely

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

您正在尝试使用basic_string<unsigned short, base::string16_char_traits>但不是来自标准库,该libstdc++.so库仅包含basic_string<char>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-templatesthen you may need an explicit instantiation.

该专业化必须用于您正在使用的库之一,因此您应该找出哪个库并确保正确链接到它。如果您正在编译,-fno-implicit-templates那么您可能需要一个显式实例化。

It might help to look for base::string16_char_traitsto 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.

base::string16_char_traits寻找实例化的来源可能会有所帮助。快速谷歌建议它来自 Chromium 库,所以也许你没有链接到它,或者你可能需要稍后将该库放在链接命令中。