gcc Link order of libraries

本文介绍了编译链接过程中库文件正确顺序的重要性。遵循从左到右的原则,确保定义在先使用在后,避免未定义符号错误。当多个库相互依赖时,正确的顺序同样关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 URL :http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html

 

 

2.7.1 Link order of libraries

The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the command line. This means that a library containing the definition of a function should appear after any source files or object files which use it. This includes libraries specified with the short-cut -l option, as shown in the following command:
$ gcc -Wall calc.c -lm -o calc (correct order)

With some linkers the opposite ordering (placing the -lm option before the file which uses it) would result in an error,
$ cc -Wall -lm calc.c -o calc (incorrect order)
main.o: In function `main':
main.o(.text+0xf): undefined reference to `sqrt'

because there is no library or object file containing sqrt after ‘calc.c’. The option -lm should appear after the file ‘calc.c’.

When several libraries are being used, the same convention should be followed for the libraries themselves. A library which calls an external function defined in another library should appear before the library containing the function.

For example, a program ‘data.c’ using the GNU Linear Programming library ‘libglpk.a’, which in turn uses the math library ‘libm.a’, should be compiled as,
$ gcc -Wall data.c -lglpk -lm

since the object files in ‘libglpk.a’ use functions defined in ‘libm.a’.

Most current linkers will search all libraries, regardless of order, but since some do not do this it is best to follow the convention of ordering libraries from left to right.

This is worth keeping in mind if you ever encounter unexpected problems with undefined references, and all the necessary libraries appear to be present on the command line.

aurora@aurora:~/rs485_static_ws$ catkin_make \ > -DCMAKE_PREFIX_PATH=/opt/ros/noetic_static \ > -DCMAKE_BUILD_TYPE=Release \ > -DBoost_USE_STATIC_LIBS=ON Base path: /home/aurora/rs485_static_ws Source space: /home/aurora/rs485_static_ws/src Build space: /home/aurora/rs485_static_ws/build Devel space: /home/aurora/rs485_static_ws/devel Install space: /home/aurora/rs485_static_ws/install #### #### Running command: "cmake /home/aurora/rs485_static_ws/src -DCMAKE_PREFIX_PATH=/opt/ros/noetic_static -DCMAKE_BUILD_TYPE=Release -DBoost_USE_STATIC_LIBS=ON -DCATKIN_DEVEL_PREFIX=/home/aurora/rs485_static_ws/devel -DCMAKE_INSTALL_PREFIX=/home/aurora/rs485_static_ws/install -G Unix Makefiles" in "/home/aurora/rs485_static_ws/build" #### CMake Warning (dev) in CMakeLists.txt: No project() command is present. The top-level CMakeLists.txt file must contain a literal, direct call to the project() command. Add a line of code such as project(ProjectName) near the top of the file, but after cmake_minimum_required(). CMake is pretending there is a "project(Project)" command on the first line. This warning is for project developers. Use -Wno-dev to suppress it. -- The C compiler identification is GNU 9.4.0 -- The CXX compiler identification is GNU 9.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Using CATKIN_DEVEL_PREFIX: /home/aurora/rs485_static_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic_static -- This workspace overlays: /opt/ros/noetic_static -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Found PY_em: /usr/lib/python3/dist-packages/em.py -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/aurora/rs485_static_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Found Threads: TRUE -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 1 packages in topological order: -- ~~ - rs485_node -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'rs485_node' -- ==> add_subdirectory(rs485_node) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- Found Boost: /usr/lib/aarch64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found version "1.71.0") found components: system filesystem thread -- rs485_node: 3 messages, 0 services -- Configuring done -- Generating done -- Build files have been written to: /home/aurora/rs485_static_ws/build #### #### Running command: "make -j8 -l8" in "/home/aurora/rs485_static_ws/build" #### Scanning dependencies of target std_msgs_generate_messages_cpp Scanning dependencies of target _rs485_node_generate_messages_check_deps_DualRS485Command Scanning dependencies of target _rs485_node_generate_messages_check_deps_backangle Scanning dependencies of target std_msgs_generate_messages_lisp Scanning dependencies of target std_msgs_generate_messages_nodejs Scanning dependencies of target std_msgs_generate_messages_eus Scanning dependencies of target _rs485_node_generate_messages_check_deps_RS485Command Scanning dependencies of target std_msgs_generate_messages_py [ 0%] Built target std_msgs_generate_messages_cpp [ 0%] Built target std_msgs_generate_messages_lisp [ 0%] Built target std_msgs_generate_messages_nodejs [ 0%] Built target std_msgs_generate_messages_eus [ 0%] Built target std_msgs_generate_messages_py [ 0%] Built target _rs485_node_generate_messages_check_deps_backangle [ 0%] Built target _rs485_node_generate_messages_check_deps_DualRS485Command [ 0%] Built target _rs485_node_generate_messages_check_deps_RS485Command Scanning dependencies of target rs485_node_generate_messages_lisp Scanning dependencies of target rs485_node_generate_messages_nodejs Scanning dependencies of target rs485_node_generate_messages_py Scanning dependencies of target rs485_node_generate_messages_cpp Scanning dependencies of target rs485_node_generate_messages_eus [ 12%] Generating Javascript code from rs485_node/DualRS485Command.msg [ 12%] Generating Lisp code from rs485_node/RS485Command.msg [ 12%] Generating Lisp code from rs485_node/backangle.msg [ 16%] Generating Javascript code from rs485_node/RS485Command.msg [ 25%] Generating EusLisp code from rs485_node/RS485Command.msg [ 25%] Generating Lisp code from rs485_node/DualRS485Command.msg [ 29%] Generating Python from MSG rs485_node/RS485Command [ 33%] Generating C++ code from rs485_node/RS485Command.msg [ 41%] Generating Python from MSG rs485_node/DualRS485Command [ 41%] Generating Python from MSG rs485_node/backangle [ 45%] Generating Javascript code from rs485_node/backangle.msg [ 50%] Generating EusLisp code from rs485_node/DualRS485Command.msg [ 50%] Built target rs485_node_generate_messages_lisp [ 54%] Generating C++ code from rs485_node/DualRS485Command.msg [ 58%] Generating EusLisp code from rs485_node/backangle.msg [ 58%] Built target rs485_node_generate_messages_nodejs [ 62%] Generating EusLisp manifest code for rs485_node [ 66%] Generating C++ code from rs485_node/backangle.msg [ 70%] Generating Python msg __init__.py for rs485_node [ 70%] Built target rs485_node_generate_messages_cpp [ 70%] Built target rs485_node_generate_messages_eus [ 70%] Built target rs485_node_generate_messages_py Scanning dependencies of target rs485_node_generate_messages Scanning dependencies of target rs485_node_lib [ 70%] Built target rs485_node_generate_messages [ 75%] Building CXX object rs485_node/CMakeFiles/rs485_node_lib.dir/src/serial_port.cpp.o [ 79%] Building CXX object rs485_node/CMakeFiles/rs485_node_lib.dir/src/protocol_handler.cpp.o [ 83%] Linking CXX static library /home/aurora/rs485_static_ws/devel/lib/librs485_node_lib.a [ 83%] Built target rs485_node_lib Scanning dependencies of target angle_publisher_node Scanning dependencies of target rs485_node [ 91%] Building CXX object rs485_node/CMakeFiles/angle_publisher_node.dir/src/angle_publisher_node.cpp.o [ 91%] Building CXX object rs485_node/CMakeFiles/rs485_node.dir/src/rs485_node.cpp.o In file included from /opt/ros/noetic_static/include/ros/ros.h:40, from /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:1: /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp: In lambda function: /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:65:22: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 9 has type ‘rs485_node::backangle_<std::allocator<void> >::_circleAngle_type’ {aka ‘unsigned int’} [-Wformat=] 65 | ROS_INFO("电机id=%d, 其单圈圈角度读数为%ld", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 66 | m.id, m.circleAngle); | ~~~~~~~~~~~~~ | | | rs485_node::backangle_<std::allocator<void> >::_circleAngle_type {aka unsigned int} /opt/ros/noetic_static/include/ros/console.h:351:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’ 351 | ole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__) | ^~~~~~~~~~~ /opt/ros/noetic_static/include/ros/console.h:390:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’ 390 | ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \ | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /opt/ros/noetic_static/include/ros/console.h:575:35: note: in expansion of macro ‘ROS_LOG_COND’ 575 | #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__) | ^~~~~~~~~~~~ /opt/ros/noetic_static/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’ 110 | #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__) | ^~~~~~~ /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:65:13: note: in expansion of macro ‘ROS_INFO’ 65 | ROS_INFO("电机id=%d, 其单圈圈角度读数为%ld", | ^~~~~~~~ /home/aurora/rs485_static_ws/src/rs485_node/src/rs485_node.cpp:65:65: note: format string is defined here 65 | ROS_INFO("电机id=%d, 其单圈圈角度读数为%ld", | ~~^ | | | long int | %d [ 95%] Linking CXX executable /home/aurora/rs485_static_ws/devel/lib/rs485_node/rs485_node [100%] Linking CXX executable /home/aurora/rs485_static_ws/devel/lib/rs485_node/angle_publisher_node /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(pthread_create.o): in function `allocate_stack': /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:525: undefined reference to `_dl_stack_flags' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:525: undefined reference to `_dl_stack_flags' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:647: undefined reference to `_dl_stack_flags' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/allocatestack.c:647: undefined reference to `_dl_stack_flags' /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(nptl-init.o): in function `__pthread_initialize_minimal_internal': /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:335: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:335: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:344: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:360: undefined reference to `_dl_init_static_tls' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:362: undefined reference to `_dl_wait_lookup_done' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:360: undefined reference to `_dl_init_static_tls' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:362: undefined reference to `_dl_wait_lookup_done' /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(nptl-init.o): in function `__pthread_get_minstack': /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:393: undefined reference to `_dl_pagesize' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/nptl-init.c:393: undefined reference to `_dl_pagesize' /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(unwind.o): in function `_jmpbuf_sp': /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' collect2: error: ld returned 1 exit status make[2]: *** [rs485_node/CMakeFiles/rs485_node.dir/build.make:111: /home/aurora/rs485_static_ws/devel/lib/rs485_node/rs485_node] Error 1 make[1]: *** [CMakeFiles/Makefile2:442: rs485_node/CMakeFiles/rs485_node.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libpthread.a(unwind.o): in function `_jmpbuf_sp': /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' /usr/bin/ld: /build/glibc-fci4Zo/glibc-2.31/nptl/../sysdeps/aarch64/jmpbuf-offsets.h:52: undefined reference to `__pointer_chk_guard_local' collect2: error: ld returned 1 exit status make[2]: *** [rs485_node/CMakeFiles/angle_publisher_node.dir/build.make:110: /home/aurora/rs485_static_ws/devel/lib/rs485_node/angle_publisher_node] Error 1 make[1]: *** [CMakeFiles/Makefile2:855: rs485_node/CMakeFiles/angle_publisher_node.dir/all] Error 2 make: *** [Makefile:141: all] Error 2 Invoking "make -j8 -l8" failed
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值