6
votes

I have the following code

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{
  printf("MySQL client version: %s\n", mysql_get_client_info());

  exit(0);
}

when i try to compile it using

gcc mysqldb.c -o mysql -I/usr/include/mysql -lmysqlclient 

i get an error saying fatal error mysql.h:No such file or directory. how can i successfully compile and run the code

4

4 Answers

11
votes

I do not know if there is some variation in your operation system. Mine is Arch Linux, and I have installed mariaDB. Within the package, there is a program called 'mysql_config' which can provide the right way of compile your program. By runnig

$ mysql_config --help
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
    --cflags         [-I/usr/include/mysql]
    --include        [-I/usr/include/mysql]
    --libs           [-L/usr/lib -lmysqlclient -lpthread -lz -lm -lssl   -lcrypto -ldl]
    --libs_r         [-L/usr/lib -lmysqlclient_r -lpthread -lz -lm -lssl -lcrypto -ldl]
    --plugindir      [/usr/lib/mysql/plugin]
    --socket         [/run/mysqld/mysqld.sock]
    --port           [0]
    --version        [10.0.17]
    --libmysqld-libs [-L/usr/lib -lmysqld]
    --variable=VAR   VAR is one of:
            pkgincludedir [/usr/include/mysql]
            pkglibdir     [/usr/lib]
            plugindir     [/usr/lib/mysql/plugin]

you can see the control flags of the program. With your program, i used the following:

$gcc main.c -o main $(mysql_config --libs --cflags)

and then, by runnig the the new program 'main'

$./main
MySQL client version: 10.0.17-MariaDB

which clearly worked out!

So, i'm sure that there a few others ways of doing this, but now this is fine for me.

Tip

Run the command

$mysql_config --libs --cflags

to see the exacly flags that mysql_config produces. Enjoy!

10
votes

I come across the same error today, turn out that I forget to install package libmysqlclient-dev. After I install it with

sudo apt install libmysqlclient-dev

the error went away.

8
votes

Check that /usr/include/mysql/mysql.h exists. If you have installed the header files somewhere else (say /opt/mysql/include), add that location with -I/opt/mysql/include.

1
votes

I had the same problem, but fortunately the command sudo apt install libmysqlclient-dev as specified by user4713908 fixed it.

I juat had to specify the path in my makefile as

gcc -o database1 chapter5_1.c -I/usr/include/mysql

I'm using Kali Linux