我正在尝试在ubuntu 12.04中安装c ++和mysql之间的连接。我安装了mysql-client,mysql-server,libmysqlclient15-dev,libmysql ++ - dev。但是当我尝试编译代码时,我得到了错误:mysql.h there is no such file
。我看着文件夹,有mysql.h文件,我无法理解为什么它找不到它。这是我的代码:
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
//set the password for mysql server here
char *password = "*********"; /* set me first */
char *database = "Real_flights";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
它工作,但现在我面临另一个错误,如:
mysql.c: In function ‘main’:
mysql.c:21: warning: incompatible implicit declaration of built-in function ‘exit’
mysql.c:27: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/ccinQBp8.o: In function `main':
mysql.c:(.text+0x3e): undefined reference to `mysql_init'
mysql.c:(.text+0x5e): undefined reference to `mysql_real_connect'
mysql.c:(.text+0x70): undefined reference to `mysql_error'
mysql.c:(.text+0xa5): undefined reference to `mysql_query'
mysql.c:(.text+0xb7): undefined reference to `mysql_error'
mysql.c:(.text+0xe7): undefined reference to `mysql_use_result'
mysql.c:(.text+0x11c): undefined reference to `mysql_fetch_row'
mysql.c:(.text+0x133): undefined reference to `mysql_free_result'
mysql.c:(.text+0x141): undefined reference to `mysql_close'
collect2: ld returned 1 exit status
来自mysql.h
Ubuntu软件包的libmysqlclient-dev
文件位于/usr/include/mysql/mysql.h
。
这不是编译器的标准搜索路径,但/usr/include
是。
您通常在代码中使用mysql.h
标头,如下所示:
#include <mysql/mysql.h>
如果您不想在源中指定目录偏移量,可以将-I
标志传递给gcc(如果这就是您正在使用的那个)以指定其他包含搜索目录,然后您不需要更改现有的码。
例如。
gcc -I/usr/include/mysql ...
只是用
$ apt-get install libmysqlclient-dev
这会自动拉出最新的libmysqlclient18-dev
我已经看到旧版本的libmysqlclient-dev(如15)将mysql.h置于奇怪的位置,例如: / usr / local / include等
否则,只是做一个
$ find /usr/ -name 'mysql.h'
并在您的make文件中放置带有-I标志的mysql.h
的文件夹路径。不干净但会起作用。
对于CentOS / RHEL:
yum install mysql-devel -y
我想你可以尝试这个gcc -I / usr / include / mysql * .c -L / usr / lib / mysql -lmysqlclient -o *
对于那些使用Eclipse IDE的人。
在安装了完整的MySQL与mysql客户端和mysql服务器以及任何mysql开发库之后,
您需要告诉Eclipse IDE以下内容
这是你如何去做的。
添加mysql.h
1. GCC C编译器 - >包含 - >包含路径(-l)然后单击+并添加到mysql.h的路径在我的情况下它是/ usr / include / mysql
要添加mysqlclient库和搜索路径到mysqlclient库,请参阅步骤3和4。
2. GCC C链接器 - >库 - >库(-l)然后单击+并添加mysqlcient
3. GCC C链接器 - >库 - >库搜索路径(-L)然后单击+并添加搜索路径到mysqlcient。在我的例子中,它是/ usr / lib64 / mysql,因为我使用的是64位Linux操作系统和64位MySQL数据库。
否则,如果您使用的是32位Linux操作系统,您可能会发现它位于/ usr / lib / mysql