gcc 不会编译和运行 MySQL C 库

发布于 2024-09-13 08:03:19 字数 425 浏览 5 评论 0 原文

#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

#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

im trying to execute this test program from terminal but get the following error message:

/tmp/cceEmI0I.o: In function main': MySQL-Test.c:(.text+0xa): undefined reference tomysql_get_client_info'

what is wrong? my system is ubuntu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一向肩并 2024-09-20 08:03:19

MySQL 附带一个名为 mysql_config 的特殊脚本。它为您提供了编译 MySQL 客户端并将其连接到 MySQL 数据库服务器的有用信息。

传递 --libs 选项 - 与 MySQL 客户端库链接所需的库和选项。

$ mysql_config --libs

典型输出:

-L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto

现在您可以将其添加到编译/链接行中:

gcc -o mysql-test MySQL-Test.c $(mysql_config --libs)

MySQL comes with a special script called mysql_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.

$ mysql_config --libs

Typical Output:

-L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto

Now you can add this to your compile/link line:

gcc -o mysql-test MySQL-Test.c $(mysql_config --libs)
荒人说梦 2024-09-20 08:03:19

您需要 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.

始于初秋 2024-09-20 08:03:19

对于 Linux 上的 Netbeans 使用

添加以下行

# These are the flags that gcc requires in order to link correctly against our installed 
# client packages
MYSQL_LIBS := $(shell mysql_config --libs)

打开您的 make 文件 (MakeFile) 并在环境块正下方

。然后右键单击您的项目节点,选择“属性”、“构建”并将 $(MYSQL_LIBS) 添加到“其他选项”参数。

For uses of Netbeans on Linux

Open you make file (MakeFile) and add the following lines

# These are the flags that gcc requires in order to link correctly against our installed 
# client packages
MYSQL_LIBS := $(shell mysql_config --libs)

right below the Environment block.

Then right click on your project node , select Properties, Build and add $(MYSQL_LIBS) to the Additional options parameter.

栀梦 2024-09-20 08:03:19

您没有链接到库。使用: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.

不回头走下去 2024-09-20 08:03:19

这不是编译错误。这是一个链接错误。

添加 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.

没有伤那来痛 2024-09-20 08:03:19

您忘记链接 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.

握住我的手 2024-09-20 08:03:19

也许迟到了,但对我有用
如果您使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文