无法找到mysql.h文件

问题描述 投票:28回答:7

我正在尝试在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
c++ mysql ubuntu-12.04 mysql-connector
7个回答
48
投票

来自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 ...

23
投票

只是用

$ apt-get install libmysqlclient-dev 

这会自动拉出最新的libmysqlclient18-dev

我已经看到旧版本的libmysqlclient-dev(如15)将mysql.h置于奇怪的位置,例如: / usr / local / include等

否则,只是做一个

$ find /usr/ -name 'mysql.h' 

并在您的make文件中放置带有-I标志的mysql.h的文件夹路径。不干净但会起作用。


14
投票

对于CentOS / RHEL:

yum install mysql-devel -y

3
投票

您可能没有包含mysql头的路径,我可以在几个unix系统上的/ usr / include / mysql中找到它。请参阅this post,它可能会有所帮助。

顺便说一句,与上面的that guy问题有关,关于合成配置。可以在〜/ .vimrc中添加以下内容:

let b:syntastic_c_cflags = '-I/usr/include/mysql'

你总是可以在github上查看开发人员的wiki page。请享用!


2
投票

你必须让编译器知道mysql.h文件的位置。这可以通过在编译之前给出标题的路径来完成。在IDE中,您有一个设置,您可以在其中提供这些路径。

这个link为您提供了有关编译时使用哪些选项的更多信息。

第二个问题您需要链接库。链接器需要知道库文件的位置,它们具有您使用的mysql函数的实现。

这个link为您提供了有关如何链接库的更多信息。


2
投票

我想你可以尝试这个gcc -I / usr / include / mysql * .c -L / usr / lib / mysql -lmysqlclient -o *


0
投票

对于那些使用Eclipse IDE的人。

在安装了完整的MySQL与mysql客户端和mysql服务器以及任何mysql开发库之后,

您需要告诉Eclipse IDE以下内容

  • 哪里可以找到mysql.h
  • 哪里可以找到libmysqlclient库
  • 搜索libmysqlclient库的路径

这是你如何去做的。

添加mysql.h

1. GCC C编译器 - >包含 - >包含路径(-l)然后单击+并添加到mysql.h的路径在我的情况下它是/ usr / include / mysql

enter image description here

要添加mysqlclient库和搜索路径到mysqlclient库,请参阅步骤3和4。

2. GCC C链接器 - >库 - >库(-l)然后单击+并添加mysqlcient

enter image description here

3. GCC C链接器 - >库 - >库搜索路径(-L)然后单击+并添加搜索路径到mysqlcient。在我的例子中,它是/ usr / lib64 / mysql,因为我使用的是64位Linux操作系统和64位MySQL数据库。

否则,如果您使用的是32位Linux操作系统,您可能会发现它位于/ usr / lib / mysql

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.