Undefined symbol "_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev" の解決策 ― 2023年01月20日 13時02分47秒
.o オブジェクトを作成し、ld --shared
で .so ファイルを生成。.so ファイルを渡して、プログラムを作ろうとしたらリンクエラーが出てる。
どのライブラリが必要か分からなかった。そこで、
ld: error: lib???.so: undefined reference to std::__1::basic_strin
g<char, std::__1::char_traits<char>, std::__1::allocator<char>
>::basic_string(std::__1::basic_string<char, std::__1::char_traits
<char>, std::__1::allocator<char> > const&) [--no-allow-shlib-u
ndefined]
ld --shared --allow-shlib-undefined
を用いて、プログラムを強制的に生成させた。
その後プログラムを実行しようとしたら、大量のエラーで起動失敗。そのうちの一つがこれ。
C++ のシンボル名は C 言語と違い C++filt を通さないと見られない。
Undefined symbol "_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev"
結局、同じようなシンボルでリンクが失敗している。まあ、std::string なので、システム関連のライブラリが必要なのだろう。当該環境は FreeBSD 12.4-RELEASE で clang 13.0.0。そこで、システムが提供しているライブラリは、/lib 以下にある。ここを find で目星を付けて、一つずつ試す。最終的に
% c++filt
_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()
-lc++
を追加したら、無事にリンク完了。
最近のコメント