gcc 不会编译和运行 MySQL C 库
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
printf("MySQL client version: %s\n", mysql_get_client_info());
}
~$ gcc -o mysql-test MySQL-Test.c
我尝试从终端执行此测试程序,但收到以下错误消息:
/tmp/cceEmI0I.o: In function main': MySQL-Test.c: (.text+0xa): 未定义引用 mysql_get_client_info'
有什么问题吗?我的系统是ubuntu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
MySQL
附带一个名为mysql_config
的特殊脚本。它为您提供了编译 MySQL 客户端并将其连接到 MySQL 数据库服务器的有用信息。传递
--libs
选项 - 与 MySQL 客户端库链接所需的库和选项。典型输出:
现在您可以将其添加到编译/链接行中:
MySQL
comes with a special script calledmysql_config
. It provides you with useful information for compiling your MySQL client and connecting it to MySQL database server.Pass
--libs
option - Libraries and options required to link with the MySQL client library.Typical Output:
Now you can add this to your compile/link line:
您需要 gcc -o mysql-test MySQL-Test.c -L/usr/local/mysql/lib -lmysqlclient -lz
替换
-L/usr/local/mysql/lib
code> 与您的客户端库所在的位置(如果它尚未在您的 libpath 中)请参阅 MySql 构建客户端的说明。
You need
gcc -o mysql-test MySQL-Test.c -L/usr/local/mysql/lib -lmysqlclient -lz
Replace
-L/usr/local/mysql/lib
with wherever you client library is (if it isn't already in your libpath)See the MySql instructions for building clients.
对于 Linux 上的 Netbeans 使用
添加以下行
打开您的 make 文件 (MakeFile) 并在环境块正下方
。然后右键单击您的项目节点,选择“属性”、“构建”并将
$(MYSQL_LIBS)
添加到“其他选项”参数。For uses of Netbeans on Linux
Open you make file (MakeFile) and add the following lines
right below the Environment block.
Then right click on your project node , select Properties, Build and add
$(MYSQL_LIBS)
to the Additional options parameter.您没有链接到库。使用:gcc -llibrarygoeshere -o mysql-test MySQL-Test.c
有关与 gcc 链接的更多信息,请参阅此处。
You are not linking to the libraries. Use:
gcc -llibrarygoeshere -o mysql-test MySQL-Test.c
See here for more information about linking with gcc.
这不是编译错误。这是一个链接错误。
添加 mysql 库以使用选项
-lmysql
创建可执行文件应该可以解决问题。It is not a compilation error. It is a link error.
Add the mysql library to create your executable with option
-lmysql
should do the trick.您忘记链接 MySQL 库。
尝试将
-lmysql
添加到您的编译行。请参阅 http://www.adp-gmbh.ch/cpp/gcc/create_lib .html 了解更多信息。
You forgot to link against the MySQL library.
Try adding
-lmysql
to your compilation line.See http://www.adp-gmbh.ch/cpp/gcc/create_lib.html for more information.
也许迟到了,但对我有用
如果您使用 IDE,则应该将该库链接到您的项目。
我在 ubuntu 12.4 64x 上使用
CodeBlocks
。要链接库,您应该转到“项目”->“构建选项->链接器设置并添加库。这是我的lib路径:/usr/lib/x86_64-linux-gnu/libmysqlclient.so
希望有用...
Maybe late but worked for me
If you are using an IDE you should link the library to your project.
I am using
CodeBlocks
on ubuntu 12.4 64x. For linking the library, you should go to Project -> Build options -> linker settings and add the library. this is my lib path :/usr/lib/x86_64-linux-gnu/libmysqlclient.so
Hope be useful...