【问题标题】:mysql.h file can't be found找不到 mysql.h 文件
【发布时间】:2013-01-30 12:39:25
【问题描述】:

我正在尝试在 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


    【解决方案1】:

    来自libmysqlclient-dev Ubuntu 软件包的mysql.h 文件位于/usr/include/mysql/mysql.h

    这不是编译器的标准搜索路径,但 /usr/include 是。

    您通常会在代码中使用 mysql.h 标头,如下所示:

    #include <mysql/mysql.h>
    

    如果您不想在源代码中指定目录偏移量,则可以将-I 标志传递给 gcc(如果您正在使用)以指定额外的包含搜索目录,然后您就不会需要更改您现有的代码。

    例如。

    gcc -I/usr/include/mysql ...
    

    【讨论】:

    • 我正在尝试查找 gcc.conf 文件以将 -I/usr/include/mysql 添加到搜索路径,以便 syntastic(vim 语法检查器)可以找到头文件。这可能/推荐吗?
    • @mattyTpain 我不熟悉语法,但我认为它至少应该支持搜索/usr/include。您应该使用 mysql/mysql.h 进行包含,因此不应该要求在 /usr/include/mysql 中搜索合成词
    • 尝试添加这个,但仍然没有成功。我正在尝试从源代码编译 pdo_mysql 并安装了 mariadb-devel 包。完整命令失败:cc -I -I/usr/includ^Cmysql -I. -I/tmp/PDO_MYSQL-1.0.2 -DPHP_ATOM_INC -I/tmp/PDO_MYSQL-1.0.2/include -I/tmp/PDO_MYSQL-1.0.2/main -I/tmp/PDO_MYSQL-1.0.2 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /tmp/PDO_MYSQL-1.0.2/pdo_mysql.c -fPIC -DPIC -o .libs/pdo_mysql.o
    【解决方案2】:

    只需使用

    $ apt-get install libmysqlclient-dev 
    

    会自动拉取最新的libmysqlclient18-dev

    我见过旧版本的 libmysqlclient-dev(如 15)将 mysql.h 放在奇怪的位置,例如/usr/local/include 等。

    否则,就做一个

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

    并将带有 -I 标志的 mysql.h 的文件夹路径放入您的 make 文件中。不干净,但会工作。

    【讨论】:

      【解决方案3】:

      对于 CentOS/RHEL:

      yum install mysql-devel -y
      

      【讨论】:

        【解决方案4】:

        这对我有用

        $ gcc dbconnect.c -o dbconnect -lmysqlclient
        $ ./dbconnect
        

        -lmysqlclient 是必须的。

        我建议使用以下符号而不是使用 -I 编译标志。

        #include <mysql/mysql.h>
        

        【讨论】:

          【解决方案5】:

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

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

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

          您可以随时查看 github 上开发人员的wiki page。享受吧!

          【讨论】:

            【解决方案6】:

            你必须让编译器知道在哪里可以找到 mysql.h 文件。这可以通过在编译之前给出头文件的路径来完成。在 IDE 中,您可以设置这些路径。

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

            关于你的第二个问题 您需要链接库。链接器需要知道库文件在哪里,哪些是您使用的 mysql 函数的实现。

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                【解决方案8】:

                对于那些使用 Eclipse IDE 的人。

                将完整的 MySQL 连同 mysql 客户端mysql 服务器 以及任何 mysql dev 库一起安装后,

                您需要告诉 Eclipse IDE 以下内容

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

                这就是你如何去做的。

                添加 mysql.h

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

                要添加 mysqlclient 库和 搜索路径mysqlclient 库的位置,请参见步骤 34

                2。 GCC C Linker -> Libraries -> Libraries(-l) 然后点击 + 并添加 mysqlcient

                3。 GCC C Linker -> Libraries -> Library search path (-L) 然后点击 + 并将 search path 添加到 mysqlcient。就我而言,它是 /usr/lib64/mysql 因为我使用的是 64 位 Linux 操作系统64 位 MySQL 数据库。

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

                【讨论】:

                  【解决方案9】:

                  这对我有用

                  yum install mysql
                  

                  它会安装mysql客户端,然后

                  pip install mysqlclient
                  

                  【讨论】:

                    猜你喜欢
                    • 2016-01-29
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-05-16
                    • 2022-08-15
                    • 1970-01-01
                    • 2012-10-21
                    • 2019-10-28
                    • 2021-12-26
                    相关资源
                    最近更新 更多