mysql.h file can't be found
Asked Answered
A

10

47

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
Addington answered 30/1, 2013 at 12:39 Comment(0)
H
70

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 ...
Hi answered 30/1, 2013 at 13:0 Comment(4)
I am trying to find the gcc.conf file to add -I/usr/include/mysql to the search path so syntastic (vim syntax checker) can find the header file. Is this possible/recommended?Addi
@mattyTpain I'm not familiar with syntastic, but I'd think that it should at least support searching /usr/include. You should do includes with mysql/mysql.h so shouldn't require syntastic to search in /usr/include/mysqlHi
Tried adding this and still not having success. I am trying to compile pdo_mysql from source and mariadb-devel package is installed. Full command failing: 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 Dispassion
What if I want to do server dev? E.g. #include <mysql/plugin_auth.h>?Kynthia
M
42

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.

Monocarpic answered 17/12, 2013 at 12:42 Comment(0)
B
23

For CentOS/RHEL:

yum install mysql-devel -y
Braziel answered 21/3, 2014 at 18:27 Comment(0)
D
7

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>
Durban answered 13/5, 2020 at 7:50 Comment(0)
D
4

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.

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

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

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

Drumhead answered 25/3, 2015 at 23:0 Comment(0)
E
3

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

Echelon answered 4/3, 2014 at 0:17 Comment(0)
A
2

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.

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

To your second problem You 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.

This link gives you more info on how to link libraries.

Archicarp answered 30/1, 2013 at 12:44 Comment(0)
I
2

For those who are using Eclipse IDE.

After installing the full MySQL together with mysql client and mysql server and any mysql dev libraries,

You will need to tell Eclipse IDE about the following

  • Where to find mysql.h
  • Where to find libmysqlclient library
  • The path to search for libmysqlclient library

Here is how you go about it.

To Add mysql.h

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

enter image description here

To add mysqlclient library and search path to where mysqlclient library see steps 3 and 4.

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

enter image description here

3. GCC C Linker -> Libraries -> Library search path (-L) then click + and add search path to mysqlcient. In my case it was /usr/lib64/mysql because I am using a 64 bit Linux OS and a 64 bit MySQL Database.

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

enter image description here

Indentation answered 16/4, 2019 at 10:26 Comment(0)
S
1

This worked for me

yum install mysql

It will install mysql client and then

pip install mysqlclient
Stodgy answered 15/2, 2021 at 10:30 Comment(0)
T
-2

just include the path in your project.cpp file, along with all the included files.

example: include "/usr/local/mysql/include/mysql.h"

Tamartamara answered 10/1 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.