C++ 找不到 mysql.h 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14604228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:30:12  来源:igfitidea点击:

mysql.h file can't be found

c++mysqlubuntu-12.04mysql-connector

提问by Begayim Muratalina

i'm trying to install connection between c++ and mysql in ubuntu 12.04. i've installed mysql-client, mysql-server, libmysqlclient15-dev, libmysql++-dev. but when i try to compile the code i got the error: mysql.h there is no such file. i looked in the folders, there is mysql.h file, i can't understand why it can't find it. here is my code:

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

it's worked, but now i'm facing another error like :

它有效,但现在我面临另一个错误,例如:

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

回答by Austin Phillips

The mysql.hfile from the libmysqlclient-devUbuntu package is located at /usr/include/mysql/mysql.h.

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

This is not a standard search path for compilers, however /usr/includeis.

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

You'd typically use the mysql.hheader in your code like this:

您通常会mysql.h像这样在代码中使用标头:

#include <mysql/mysql.h>

If you don't want to specify the directory offset in your source, you can pass the -Iflag to gcc (If that's what you are using) to specify an additional include search directory, and then you wouldn't need to change your existing code.

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

eg.

例如。

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

回答by Waqas

just use

只是使用

$ apt-get install libmysqlclient-dev 

which will automatically pull the latest libmysqlclient18-dev

这将自动拉取最新的 libmysqlclient18-dev

I have seen older versions of libmysqlclient-dev (like 15) puts the mysql.h in weird locations e.g. /usr/local/include etc.

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

otherwise, just do a

否则,只需做一个

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

and put the folder path of your mysql.hwith -I flag in your make file. Not clean but will work.

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

回答by Ryan K

For CentOS/RHEL:

对于 CentOS/RHEL:

yum install mysql-devel -y

回答by Ryan K

You probably don't included the path to mysql headers, which can be found at /usr/include/mysql, on several unix systems I think. See this post, it may be helpfull.

您可能没有在我认为的几个 unix 系统上包含 mysql 头文件的路径,该路径可以在 /usr/include/mysql 中找到。看到这个帖子,可能会有所帮助。

By the way, related with the question of that guyabove, about syntastic configuration. One can add the following to your ~/.vimrc:

顺便说一句,与上面那个人的问题有关,关于语法配置。可以将以下内容添加到您的 ~/.vimrc 中:

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

and you can always check the wiki pageof the developers on github. Enjoy!

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

回答by CharlesX

I think you can try this gcc -I/usr/include/mysql *.c -L/usr/lib/mysql -lmysqlclient -o *

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

回答by Sunil D S

You have to let the compiler know where the mysql.h file can be found. This can be done by giving the path to the header before compiling. In IDEs you have a setting where you can give these paths.

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

This linkgives you more info on what options to use while compiling.

链接为您提供有关编译时要使用的选项的更多信息。

To your second problemYou need to link the libraries. The linker needs to know where the library files are which has the implementation for the mysql functions that you use.

你的第二个问题你需要链接库。链接器需要知道库文件在哪里,其中包含您使用的 mysql 函数的实现。

This linkgives you more info on how to link libraries.

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

回答by StuxNet

this worked for me

这对我有用

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

-lmysqlclient is must.

-lmysqlclient 是必须的。

and i would personally recommend to use following notation instead of using -I compilation flag.

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

#include <mysql/mysql.h>

回答by Joseph

For those who are using Eclipse IDE.

对于那些使用Eclipse IDE 的人。

After installing the full MySQL together with mysql clientand mysql serverand any mysql devlibraries,

将完整的 MySQL 与mysql 客户端mysql 服务器以及任何mysql 开发库一起安装后,

You will need to tell Eclipse IDEabout the following

您需要将以下信息告知Eclipse IDE

  • Where to find mysql.h
  • Where to find libmysqlclientlibrary
  • The pathto searchfor libmysqlclientlibrary
  • 在哪里可以找到mysql.h
  • 在哪里可以找到libmysqlclient
  • 路径搜索的libmysqlclient

Here is how you go about it.

这是你如何去做的。

To Add mysql.h

添加mysql.h

1. GCC C Compiler -> Includes -> Include paths(-l) then click + and add path to your mysql.hIn my case it was /usr/include/mysql

1. GCC C Compiler -> Includes -> Include paths(-l) 然后单击 + 并将路径添加到您的mysql.h在我的情况下它是/usr/include/mysql

enter image description here

在此处输入图片说明

To add mysqlclientlibrary and search pathto where mysqlclientlibrary see steps 3and 4.

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

2. GCC C Linker -> Libraries -> Libraries(-l) then click +and add mysqlcient

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

enter image description here

在此处输入图片说明

3. GCC C Linker -> Libraries -> Library search path (-L) then click +and add search pathto mysqlcient. In my case it was /usr/lib64/mysqlbecause I am using a 64 bit Linux OSand a 64 bit MySQLDatabase.

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

Otherwise, if you are using a 32 bit Linux OS, you may find that it is found at /usr/lib/mysql

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

enter image description here

在此处输入图片说明