编译指令的顺序

来源:互联网 发布:canonmp288清零软件 编辑:程序博客网 时间:2024/05/17 04:07
今天尝试编译 fackbook 的pmysql。

Makefile文件如下

CXXFLAGS+=-Wall -Werror -g -O3 `mysql_config --cflags` `pkg-configglib-2.0 gthread-2.0 --cflags`
LDFLAGS+=`mysql_config --libs_r` `pkg-config glib-2.0 gthread-2.0--libs`

all: pmysql

install: pmysql
  install -D pmysql${DESTDIR}/usr/bin/pmysql

clean:
  rm -f pmysql


生成的编译命令为:
g++ -Wall -Werror -g -O3 `mysql_config --cflags` `pkg-configglib-2.0 gthread-2.0 --cflags` `mysql_config --libs_r` `pkg-configglib-2.0 gthread-2.0 --libs` pmysql.cc   -o pmysql


执行make编译的时候碰到如下错误:
/tmp/ccxpuX6i.o: In function `write_g_string':
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:81: undefinedreference to `g_threads_got_initialized'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:90: undefinedreference to `g_threads_got_initialized'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:90: undefinedreference to `g_thread_functions_for_glib_use'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:81: undefinedreference to `g_thread_functions_for_glib_use'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:85: undefinedreference to `g_log'
/tmp/ccxpuX6i.o: In function `run_query(char*, st_mysql*, char*,char*)':
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:135: undefinedreference to `mysql_select_db'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:141: undefinedreference to `mysql_query'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:145: undefinedreference to `g_string_sized_new'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:146: undefinedreference to `g_string_sized_new'
/home/gaoxiaoxin/pmysql-repo/pmysql/pmysql/pmysql.cc:148: undefinedreference to `mysql_use_result'
。。。。。。


解决办法
将编译指令调整为:
g++ -Wall -Werror -g -O3 pmysql.cc   -o pmysql`mysql_config --cflags` `pkg-config glib-2.0 gthread-2.0 --cflags``mysql_config --libs_r` `pkg-config glib-2.0 gthread-2.0--libs` 
编译就成功了。

原因:
The order of arguments to the linker is significant. Usemysql-config after listing the files that need it. Thelinker will see that cpulogger.o needs mysql_init and look inlibraries listed after it for the symbol. If the libraries werelisted earlier in the arguments they won't be searched again.
也就是说 需要链接的lib什么的 需要放在要生成的 .o 文件之后, 要不链接器不会去找。
引用自
http://stackoverflow.com/questions/10970356/undefined-reference-to-mysql-init

转载注明出自高孝鑫的博客
0 0