未定义的引用`mysql_init' [英] undefined reference to `mysql_init'

查看:2698
本文介绍了未定义的引用`mysql_init'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译我的新的服务器上我的程序,但它不是现在为我工作。

I am trying to compile my program on my new server, but it's not working for me at the moment.

错误日志是:

rasmus@web01:~/c++$ make test
g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
/tmp/ccPaMZUy.o: In function `CPULogger':
/home/rasmus/c++/cpulogger.cpp:7: undefined reference to `mysql_init'
/home/rasmus/c++/cpulogger.cpp:8: undefined reference to `mysql_real_connect'
/home/rasmus/c++/cpulogger.cpp:10: undefined reference to `mysql_get_client_info'
/tmp/ccPaMZUy.o: In function `~CPULogger':
/home/rasmus/c++/cpulogger.cpp:16: undefined reference to `mysql_close'
collect2: ld returned 1 exit status
make: *** [all] Error 1

正如你能看到我的编译MySQL的反对 - 我已经检查过的mysql.h中包含路径present

As you can see i am compiling against mysql - i have checked that mysql.h is present in include paths.

我想什么?

编辑:

cpulogger.cpp拥有的#includecpulogger.h的顶部,然后cpulogger.h有这个:

cpulogger.cpp has #include "cpulogger.h" at the top, then cpulogger.h has this:

#include <iostream>
#include <fstream>
#include <mysql/mysql.h>

编译器不抱怨缺少的mysql / mysql.h中,所以这部分必须工作?

The compiler does not complain about missing mysql/mysql.h, so that part must work?

mysql_config的输出:

Output of mysql_config:

rasmus@web01:~/c++$ mysql_config --cflags --libs
-I/usr/include/mysql -DBIG_JOINS=1  -fno-strict-aliasing  -g
-L/usr/lib -lmysqlclient -lpthread -lz -lm -lrt -ldl

的Makefile:

Makefile:

all:
    g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test


test: all
    ./test

这是一个新鲜的unbuntu服务器安装与一个MySQL服务器安装就可以了。

It's a fresh unbuntu server installation with a mysql-server install on it.

[解决]:

把链接libaries在编译器的命令结束工作。

Putting linker libaries at end of compiler commands works.

all:
    g++ main.cpp logger.cpp cpulogger.cpp -o test `mysql_config --cflags --libs`

有关说明,请参见下面的答案。

See answer below for explanation.

推荐答案

参数,以连接器的顺序是显著。请使用MySQL配置的之后的列出需要的文件。连接器将看到cpulogger.o需求mysql_init,并期待在它之后列出的符号库。如果文库在参数前面列出他们将不会再搜索。

The order of arguments to the linker is significant. Use mysql-config after listing the files that need it. The linker will see that cpulogger.o needs mysql_init and look in libraries listed after it for the symbol. If the libraries were listed earlier in the arguments they won't be searched again.

这篇关于未定义的引用`mysql_init'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆