通过“未定义的引用 `boost::system::get_system_category()”链接到 boost barfs;
我在静态链接使用 boost 1.35 库的应用程序时遇到问题。我使用的是 Linux debian Lenny box,带有 G++ 4.3.2。没有 -static 的链接可以顺利进行。
具体来说,
g++ -Wall -Wextra -pedantic -ggdb3 -O0 -static -l boost_thread-mt -lboost_system-mt -lboost_program_options-mt -lssl -lpthread -l crypto main.o comandos.o utils.o tunnel.o opciones.o decode.o sysutils.o -o sapp
main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:204:undefined reference to `boost::system::get_system_category()'
/usr/include/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_posix_category()'
/usr/include/boost/system/error_code.hpp:209: undefined reference to `boost::system::get_posix_category()'
/usr/include/boost/system/error_code.hpp:210: undefined reference to `boost::system::get_system_category()'
我链接的是 boost_system-mt,它可以在我的盒子上的 /usr/lib 中找到。如果我链接到非多线程安全版本的 boost_system (-lboost_system),也会发生同样的事情
sarraga@saggy:~/sapp/src$ ls -al /usr/lib/libboost_system*a
-rw-r--r-- 1 root root 23506 2008-05-23 05:32 /usr/lib/libboost_system.a
lrwxrwxrwx 1 root root 17 2010-08-26 19:10 /usr/lib/libboost_system-gcc42-1_35.a -> libboost_system.a
lrwxrwxrwx 1 root root 20 2010-08-26 19:10 /usr/lib/libboost_system-gcc42-mt-1_35.a -> libboost_system-mt.a
-rw-r--r-- 1 root root 23506 2008-05-23 05:32 /usr/lib/libboost_system-mt.a
并且我在那里找到了未解析的符号
sarraga@saggy:~/sapp/src$ nm -C /usr/lib/libboost_system-mt.a | grep 'T.*get.*category'
00000050 T boost::system::get_posix_category()
000000b0 T boost::system::get_system_category()
strace 显示链接器打开了库
sarraga@saggy:~/sapp/src$ strace -f make 2>&1 | grep boost_system
[pid 15016] execve("/usr/bin/g++", ["g++", "-Wall", "-Wextra", "-pedantic", "-ggdb3", "-O0", "-static", "-l", "boost_thread-mt", "-lboost_system-mt", "-lboost_program_options-mt", "-lssl", "-lpthread", "-l", "crypto", "main.o", ...], [/* 41 vars */] <unfinished ...>
...
[pid 15018] open("/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libboost_system-mt.a", O_RDONLY|O_LARGEFILE) = 8
这是 debian 中 boost 的标准软件包安装,
sarraga@saggy:~/sapp/src$ dpkg -l | grep boos
ii libboost-date-time1.35-dev 1.35.0-5 set of date-time libraries based on generic programming
ii libboost-date-time1.35.0 1.35.0-5 set of date-time libraries based on generic programming
ii libboost-filesystem1.35-dev 1.35.0-5 filesystem operations (portable paths, iteration over d
ii libboost-filesystem1.35.0 1.35.0-5 filesystem operations (portable paths, iteration over d
ii libboost-graph1.35-dev 1.35.0-5 generic graph components and algorithms in C++
ii libboost-graph1.35.0 1.35.0-5 generic graph components and algorithms in C++
ii libboost-iostreams1.35-dev 1.35.0-5 Boost.Iostreams Library development files
ii libboost-iostreams1.35.0 1.35.0-5 Boost.Iostreams Library
ii libboost-program-options1.35-dev 1.35.0-5 program options library for C++
ii libboost-program-options1.35.0 1.35.0-5 program options library for C++
ii libboost-python1.35-dev 1.35.0-5 Boost.Python Library development files
ii libboost-python1.35.0 1.35.0-5 Boost.Python Library
ii libboost-regex1.35-dev 1.35.0-5 regular expression library for C++
ii libboost-regex1.35.0 1.35.0-5 regular expression library for C++
ii libboost-serialization1.35-dev 1.35.0-5 serialization library for C++
ii libboost-serialization1.35.0 1.35.0-5 serialization library for C++
ii libboost-signals1.35-dev 1.35.0-5 managed signals and slots library for C++
ii libboost-signals1.35.0 1.35.0-5 managed signals and slots library for C++
ii libboost-system1.35-dev 1.35.0-5 Operating system (e.g. diagnostics support) library
ii libboost-system1.35.0 1.35.0-5 Operating system (e.g. diagnostics support) library
ii libboost-test1.35-dev 1.35.0-5 components for writing and executing test suites
ii libboost-test1.35.0 1.35.0-5 components for writing and executing test suites
ii libboost-thread1.35-dev 1.35.0-5 portable C++ multi-threading
ii libboost-thread1.35.0 1.35.0-5 portable C++ multi-threading
ii libboost-wave1.35-dev 1.35.0-5 C99/C++ preprocessor library
ii libboost-wave1.35.0 1.35.0-5 C99/C++ preprocessor library
ii libboost1.35-dev 1.35.0-5 Boost C++ Libraries development files
ii libboost1.35-doc 1.35.0-5 Boost.org libraries documentation
我确信我错过了一个愚蠢的细节,但我找不到它。有人帮忙吗?
I'm having trouble statically linking an app which uses the boost 1.35 libraries. I'm using a linux debian Lenny box, with G++ 4.3.2. Linking without -static works without a hitch.
Specifically,
g++ -Wall -Wextra -pedantic -ggdb3 -O0 -static -l boost_thread-mt -lboost_system-mt -lboost_program_options-mt -lssl -lpthread -l crypto main.o comandos.o utils.o tunnel.o opciones.o decode.o sysutils.o -o sapp
main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:204:undefined reference to `boost::system::get_system_category()'
/usr/include/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_posix_category()'
/usr/include/boost/system/error_code.hpp:209: undefined reference to `boost::system::get_posix_category()'
/usr/include/boost/system/error_code.hpp:210: undefined reference to `boost::system::get_system_category()'
I'm linking against boost_system-mt, which is found on my box in /usr/lib. The same thing happens if I link against the non multithread-safe version of boost_system (-lboost_system)
sarraga@saggy:~/sapp/src$ ls -al /usr/lib/libboost_system*a
-rw-r--r-- 1 root root 23506 2008-05-23 05:32 /usr/lib/libboost_system.a
lrwxrwxrwx 1 root root 17 2010-08-26 19:10 /usr/lib/libboost_system-gcc42-1_35.a -> libboost_system.a
lrwxrwxrwx 1 root root 20 2010-08-26 19:10 /usr/lib/libboost_system-gcc42-mt-1_35.a -> libboost_system-mt.a
-rw-r--r-- 1 root root 23506 2008-05-23 05:32 /usr/lib/libboost_system-mt.a
And I find the unresolved symbols there
sarraga@saggy:~/sapp/src$ nm -C /usr/lib/libboost_system-mt.a | grep 'T.*get.*category'
00000050 T boost::system::get_posix_category()
000000b0 T boost::system::get_system_category()
An strace shows that the linker opens the library
sarraga@saggy:~/sapp/src$ strace -f make 2>&1 | grep boost_system
[pid 15016] execve("/usr/bin/g++", ["g++", "-Wall", "-Wextra", "-pedantic", "-ggdb3", "-O0", "-static", "-l", "boost_thread-mt", "-lboost_system-mt", "-lboost_program_options-mt", "-lssl", "-lpthread", "-l", "crypto", "main.o", ...], [/* 41 vars */] <unfinished ...>
...
[pid 15018] open("/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libboost_system-mt.a", O_RDONLY|O_LARGEFILE) = 8
It's a standard package installation of boost in debian,
sarraga@saggy:~/sapp/src$ dpkg -l | grep boos
ii libboost-date-time1.35-dev 1.35.0-5 set of date-time libraries based on generic programming
ii libboost-date-time1.35.0 1.35.0-5 set of date-time libraries based on generic programming
ii libboost-filesystem1.35-dev 1.35.0-5 filesystem operations (portable paths, iteration over d
ii libboost-filesystem1.35.0 1.35.0-5 filesystem operations (portable paths, iteration over d
ii libboost-graph1.35-dev 1.35.0-5 generic graph components and algorithms in C++
ii libboost-graph1.35.0 1.35.0-5 generic graph components and algorithms in C++
ii libboost-iostreams1.35-dev 1.35.0-5 Boost.Iostreams Library development files
ii libboost-iostreams1.35.0 1.35.0-5 Boost.Iostreams Library
ii libboost-program-options1.35-dev 1.35.0-5 program options library for C++
ii libboost-program-options1.35.0 1.35.0-5 program options library for C++
ii libboost-python1.35-dev 1.35.0-5 Boost.Python Library development files
ii libboost-python1.35.0 1.35.0-5 Boost.Python Library
ii libboost-regex1.35-dev 1.35.0-5 regular expression library for C++
ii libboost-regex1.35.0 1.35.0-5 regular expression library for C++
ii libboost-serialization1.35-dev 1.35.0-5 serialization library for C++
ii libboost-serialization1.35.0 1.35.0-5 serialization library for C++
ii libboost-signals1.35-dev 1.35.0-5 managed signals and slots library for C++
ii libboost-signals1.35.0 1.35.0-5 managed signals and slots library for C++
ii libboost-system1.35-dev 1.35.0-5 Operating system (e.g. diagnostics support) library
ii libboost-system1.35.0 1.35.0-5 Operating system (e.g. diagnostics support) library
ii libboost-test1.35-dev 1.35.0-5 components for writing and executing test suites
ii libboost-test1.35.0 1.35.0-5 components for writing and executing test suites
ii libboost-thread1.35-dev 1.35.0-5 portable C++ multi-threading
ii libboost-thread1.35.0 1.35.0-5 portable C++ multi-threading
ii libboost-wave1.35-dev 1.35.0-5 C99/C++ preprocessor library
ii libboost-wave1.35.0 1.35.0-5 C99/C++ preprocessor library
ii libboost1.35-dev 1.35.0-5 Boost C++ Libraries development files
ii libboost1.35-doc 1.35.0-5 Boost.org libraries documentation
I'm sure I'm missing an stupid detail, but I can't find it. Anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当静态链接时,链接器期望库位于包含对它们的引用的文件之后。您需要将 .o 文件移到 -l 标志之前。这个想法是,后面的文件“填充”早期文件中包含的引用——因为您的 .o 文件是最后的,链接器希望它们填充 boost_system 中任何缺失的符号,而不是反之亦然。当您动态链接时,顺序并不重要,因为直到第一次在运行时使用符号时它才会解析符号(那时它知道要在其中查找符号的库的完整列表)。
When statically linking the linker expects that libraries will come after the files containing references to them. You need to move your .o files before your -l flags. The idea is that files that come later 'fill in' references contained in earlier files -- since your .o files are last, the linker expects them to fill in any missing symbols in boost_system rather than vice versa. When you dynamically link, the order doesn't matter because it doesn't resolve a symbol until the first time it's used at runtime (and by that time it knows the full list of libraries to look for symbols in).