最近发现一个程序执行中报错,报错内容如下,这里对排查和修复方法做一个记录。
XXXX:symbol lookup error:/home/…/libpdfium.so:undefined
symbol:CRYPT_MD5Generate
先说结论,undefined symbol 是因为你的程序中使用了未实现的函数。
比方说你编译时只加入了头文件*.h
,而函数实现部分.c
,文件未编译进去,这时候就会出现undefined symbol。
还有一种情况是,在C++代码调用C语言代码也会导致出现undefined symbol
,原因是C++支持函数重载,编译器在编译函数时会将函数类型也加到编译后的代码中,而C语言并不支持函数重载,编译后的代码不包含函数类型,一般只包含函数名,这就会导致*.h
文件中定义的函数,找不到对应.c
文件中的实现。
解决办法是在*.h
文件中添加extern "C" {}
将C语言的函数声明括起来即可。 更多信息参考:C++ extern “C”的作用详解
此外附上几个排查undefined symbol
问题比较有用的命令。
编译生成动态链接库后,调用时出现:
# lichunhong @ lichunhong-ThinkPad-T470p in ~/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws on git:lichunhong/dev x [18:54:05] C:127
$ rosrun path_plan PathPlanSimulation
/home/lichunhong/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws/devel/lib/path_plan/PathPlanSimulation:
symbol lookup error: /home/lichunhong/Documents/src/effective_robotics_programming_with_ros-master/catkin_ws/src/pathPlan/lib/libpathplan.so:
undefined symbol: _ZN12ninebot_algo10AprAlgoLog9instance_E
即 symbol lookup error: libpathplan.so: undefined symbol: _ZN12ninebot_algo10AprAlgoLog9instance_E