今天编译mysql的动态库(libodbc.so)编译过了,但当我编译一个应用服务时需链接用到mysql的动态库时就报错:
/home/sc/lib/libodbc.so: undefined reference to `mysql_server_init'
/home/sc/lib/libodbc.so: undefined reference to `mysql_close'/home/sc/lib/libodbc.so: undefined reference to `mysql_affected_rows'/home/sc/lib/libodbc.so: undefined reference to `mysql_ping'/home/sc/lib/libodbc.so: undefined reference to `mysql_real_query'/home/sc/lib/libodbc.so: undefined reference to `mysql_set_character_set'/home/sc/lib/libodbc.so: undefined reference to `mysql_free_result'/home/sc/lib/libodbc.so: undefined reference to `mysql_autocommit'/home/sc/lib/libodbc.so: undefined reference to `mysql_fetch_field_direct'/home/sc/lib/libodbc.so: undefined reference to `mysql_real_connect'/home/sc/lib/libodbc.so: undefined reference to `mysql_store_result'/home/sc/lib/libodbc.so: undefined reference to `mysql_field_count'/home/sc/lib/libodbc.so: undefined reference to `mysql_commit'/home/sc/lib/libodbc.so: undefined reference to `mysql_fetch_row'/home/sc/lib/libodbc.so: undefined reference to `mysql_error'/home/sc/lib/libodbc.so: undefined reference to `mysql_server_end'/home/sc/lib/libodbc.so: undefined reference to `mysql_rollback'/home/sc/lib/libodbc.so: undefined reference to `mysql_errno'/home/sc/lib/libodbc.so: undefined reference to `mysql_init'
排错:
1. 查看动态库mysql相关的方法:nm libodbc.so|grep mysql
U mysql_affected_rows
U mysql_autocommit U mysql_close U mysql_commit U mysql_errno U mysql_error U mysql_fetch_field_direct U mysql_fetch_row U mysql_field_count U mysql_free_result U mysql_init U mysql_ping U mysql_real_connect U mysql_real_query U mysql_rollback U mysql_server_end U mysql_server_init U mysql_set_character_set U mysql_store_result其实这些方法前面的 'U' 代表:该符号未定义过,需要自其他对象文件中链接进来;
这时候在看编译mysql动态库所链接的动态库,发现了链接-L$(MYSQL_LIB),链接时(-L是代表库的路径,而我们这个系统现在根本没有这个环境变量),这个估计是同事在调不同mysql版本时改makefile加上的
将此去掉在重新编译,那些方法前面的 'U' 变成了 'T'代表:该符号放在代码段中,通常是那些全局非静态函数;