Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql.h file can't be found

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:

 /* 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
like image 446
Begayim Muratalina Avatar asked Jan 30 '13 12:01

Begayim Muratalina


People also ask

Where to find mysql h?

The mysql. h file from the libmysqlclient-dev Ubuntu package is located at /usr/include/mysql/mysql. h . This is not a standard search path for compilers, however /usr/include is.

What is Libmysqlclient Dev?

C API (libmysqlclient) C API (libmysqlclient) is a client library for C development. For C-language and SQL: for MySQL 8.0, 5.7, 5.6, 5.5. we recommend MySQL 8.0 C API.


4 Answers

The mysql.h file from the libmysqlclient-dev Ubuntu package is located at /usr/include/mysql/mysql.h.

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

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

#include <mysql/mysql.h> 

If you don't want to specify the directory offset in your source, you can pass the -I flag 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.

eg.

gcc -I/usr/include/mysql ... 
like image 60
Austin Phillips Avatar answered Sep 28 '22 08:09

Austin Phillips


just use

$ apt-get install libmysqlclient-dev  

which will automatically pull the latest 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.

otherwise, just do a

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

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

like image 39
Waqas Avatar answered Sep 28 '22 09:09

Waqas


For CentOS/RHEL:

yum install mysql-devel -y
like image 32
Ryan K Avatar answered Sep 28 '22 08:09

Ryan K


this worked for me

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

-lmysqlclient is must.

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

#include <mysql/mysql.h>
like image 25
Rahul Avatar answered Sep 28 '22 07:09

Rahul