Hi ,
I am able to compile my program , which simply connect to mysql and prints the table result. But I am unable to compile it with Makefile. Any pointer will be appreciated.

My program:

#include <mysql.h>
#include <stdio.h>

main() {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char *server = "localhost";
   char *user = "root";
   char *password = "mysqlpass"; /* set me first */
   char *database = "mysql";

   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);
}

when i simply compile it (mysql.c)
gcc mysql.c
It gives me following error
mysql.c:1:19: error: mysql.h: No such file or directory
mysql.c: In function ‘main’:
mysql.c:5: error: ‘MYSQL’ undeclared (first use in this function)
mysql.c:5: error: (Each undeclared identifier is reported only once
mysql.c:5: error: for each function it appears in.)
mysql.c:5: error: ‘conn’ undeclared (first use in this function)
mysql.c:6: error: ‘MYSQL_RES’ undeclared (first use in this function)
mysql.c:6: error: ‘res’ undeclared (first use in this function)
mysql.c:7: error: ‘MYSQL_ROW’ undeclared (first use in this function)
mysql.c:7: error: expected ‘;’ before ‘row’
mysql.c:20: warning: incompatible implicit declaration of built-in function ‘exit’
mysql.c:26: warning: incompatible implicit declaration of built-in function ‘exit’
mysql.c:33: error: ‘row’ undeclared (first use in this function)


When I compile it with
gcc -o mysqlrun $(mysql_config --cflags) mysql.c $(mysql_config --libs)
Its successfully compiled..

But my problem is How to write it into Makefile
Here is my Makefile
******************************************************************
CC= gcc
MYSQLCFLAGS= mysql_config --cflags
MYSQLLIBS= mysql_config --libs

mysqlrun:
$(CC)-o mysqlrun $(MYSQLCFLAGS) mysql.c $(MYSQLLIBS)

clean:
rm -f mysqlrun
******************************************************************

It gives me following error
gcc -o mysqlrun mysql_config --cflags mysql.c mysql_config --libs
gcc: mysql_config: No such file or directory
gcc: mysql_config: No such file or directory
cc1: error: unrecognized command line option "-fcflags"
cc1: error: unrecognized command line option "-flibs"
make: *** [mysqlrun] Error 1

Recommended Answers

All 17 Replies

you need to add the include path to mysql.h
INCLUDES="c:\mysql\include"
$(CC)-o mysqlrun $(INCLUDES) $(MYSQLCFLAGS) mysql.c $(MYSQLLIBS)

Thank you for quick reply.

I am working in Linux environment so I dont have C:\ ...
When I did locate mysql.h , its not in my system, So what should I do then ??

for linux just change c:\blabla to whatever it is on your computer.

>>When I did locate mysql.h , its not in my system
you mean its on another mounted file system? That should be ok, You will also have to do similar for the mysql libraries so that your compiler can link to them.

I am using standalone Linux, means only one OS is installed on my system.
FYI: when I am able to compile my program by giving following command
$(CC)-o mysqlrun $(MYSQLCFLAGS) mysql.c $(MYSQLLIBS) without using any INCLUDE , so it should work in Makefile without any error , Isn't it.

Another point all the mysql libraries are installed inside linux is in /usr/inluce/mysql/

but there is not any file like mysql.h.
Look following are the files which r listed there
ls /usr/include/mysql/
chardefs.h m_ctype.h my_dbug.h my_no_pthread.h mysql.h readline.h sql_state.h xmalloc.h
decimal.h m_string.h my_dir.h my_pthread.h mysql_time.h rlmbutil.h sslopt-case.h
errmsg.h my_alloc.h my_getopt.h mysql_com.h mysql_version.h rlprivate.h sslopt-longopts.h
history.h my_attribute.h my_global.h mysqld_ername.h my_sys.h rlshell.h sslopt-vars.h
keycache.h my_config.h my_list.h mysqld_error.h my_xml.h rltypedefs.h tilde.h
keymaps.h my_config_i386.h my_net.h mysql_embed.h raid.h sql_common.h typelib.h

I tried with this also, as u said..
INCLUDES="/usr/include/mysql/"
$(CC)-o mysqlrun $(INCLUDES) $(MYSQLCFLAGS) mysql.c $(MYSQLLIBS)

But still getting same error

make
gcc /usr/include/mysql/ -o mysqlrun mysql_config --cflags mysql.c mysql_config --libs
gcc: mysql_config: No such file or directory
gcc: mysql_config: No such file or directory
cc1: error: unrecognized command line option "-fcflags"
cc1: error: unrecognized command line option "-flibs"
make: *** [mysqlrun] Error 1

Did you install mysql database on your computer? If no then I think you may have to install mysql on your computer in order to get the header files etc.

$ rpm -qa | grep "mysql" .. and the ouput is

mysql-5.0.45-7.el5 -- its the mysql client
php-mysql-5.1.6-20.el5_2.1 - for php
mysql-server-5.0.45-7.el5 -- mysql server
libdbi-dbd-mysql-0.8.1a-1.2.2 - some mysql library
mysql-devel-5.0.45-7.el5 -- mysql development environment (responsible for mysql.h and all the header library)
mysql-connector-odbc-3.51.12-2.2 - mysql odbc connector

.. Now where to go .

That means I have installed , mysql client, server, and development environment on my system..

Thank you

did you try #include <mysql/mysql.h>

Yes I tried with it also, and the error I am getting now is related to mysql API's

like
mysql.c: (.text+0x35): undefined reference to `mysql_init'
mysql.c: (.text+0x77): undefined reference to `mysql_real_connect'
mysql.c: (.text+0x86): undefined reference to `mysql_error'
mysql.c: (.text+0xbf): undefined reference to `mysql_query'
mysql.c: (.text+0xce): undefined reference to `mysql_error'
mysql.c: (.text+0xff): undefined reference to `mysql_use_result'
mysql.c: (.text+0x130): undefined reference to `mysql_fetch_row'
mysql.c: (.text+0x144): undefined reference to `mysql_free_result'
mysql.c: (.text+0x14f): undefined reference to `mysql_close'
collect2: ld returned 1 exit status
make: *** [mysqlrun] Error 1

:sad:

I have solved this issue.

What exactly happens when you include mysql.h header file in a program , it searches for mysql.h header file first and two options. ie.

output of $(mysql_config --cflags) and $(mysql_config --libs)
So either you can add the output of above variables while doing gcc or you can make another variable which will add both the mysql_config macros in your Makefile.

Finally its working.

Hello folks.
I've got the same problem: i'd like include mysql.h but when i link with

gcc -o v Zrss.c -I/usr/include/mysql  -DBIG_JOINS=1 -fPIC -Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient

(the output of mysql_config -lib etc)
this is the output:

undefined reference to `mysql_connect'
collect2: ld returned 1 exit status

can you help me? thank you

Hi emma,
It seems you dont have properly installed version of mysql API.
what you need to do is to install mysql.devel RPM

after that in source file include it like
#include<mysql/mysql.h>

and let say the file name is foo.c , u have to compile it with

gcc -I/usr/include/mysql -o FOORUN $(mysql_config --cflags) foo.c $(mysql_config --libs)

I think this will solve your problem.

I've been installed yet mysqlclient1.5.
These are the directories.

emanuele@emanuele-desktop:~$ ls -l /usr/include/mysqltotale 384
-rw-r--r-- 1 root root 4050 2008-09-19 15:23 decimal.h
-rw-r--r-- 1 root root 3866 2008-09-19 15:23 errmsg.h
-rw-r--r-- 1 root root 6791 2008-09-19 15:23 keycache.h
-rw-r--r-- 1 root root 21374 2008-09-19 15:23 m_ctype.h
-rw-r--r-- 1 root root 8168 2008-09-19 15:23 m_string.h
-rw-r--r-- 1 root root 1810 2008-09-19 15:23 my_alloc.h
-rw-r--r-- 1 root root 1941 2008-09-19 15:23 my_attribute.h
-rw-r--r-- 1 root root 30798 2008-09-19 15:24 my_config.h
-rw-r--r-- 1 root root 4547 2008-09-19 15:23 my_dbug.h
-rw-r--r-- 1 root root 3460 2008-09-19 15:24 my_dir.h
-rw-r--r-- 1 root root 2962 2008-09-19 15:23 my_getopt.h
-rw-r--r-- 1 root root 43779 2008-09-19 15:23 my_global.h
-rw-r--r-- 1 root root 1466 2008-09-19 15:23 my_list.h
-rw-r--r-- 1 root root 3853 2008-09-19 15:23 my_net.h
-rw-r--r-- 1 root root 1669 2008-09-19 15:23 my_no_pthread.h
-rw-r--r-- 1 root root 27519 2008-09-19 15:23 my_pthread.h
-rw-r--r-- 1 root root 17387 2008-09-19 15:23 mysql_com.h
-rw-r--r-- 1 root root 16790 2008-09-19 15:23 mysqld_ername.h
-rw-r--r-- 1 root root 16845 2008-09-19 15:23 mysqld_error.h
-rw-r--r-- 1 root root 1175 2008-09-19 15:23 mysql_embed.h
-rw-r--r-- 1 root root 33581 2008-09-19 15:23 mysql.h
-rw-r--r-- 1 root root 2097 2008-09-19 15:23 mysql_time.h
-rw-r--r-- 1 root root 821 2008-09-19 15:23 mysql_version.h
-rw-r--r-- 1 root root 37669 2008-09-19 15:23 my_sys.h
-rw-r--r-- 1 root root 1958 2008-09-19 15:23 my_xml.h
drwxr-xr-x 4 root root 4096 2009-03-15 21:57 ndb
-rw-r--r-- 1 root root 5796 2008-09-19 15:23 raid.h
-rw-r--r-- 1 root root 1846 2008-09-19 15:23 sql_common.h
-rw-r--r-- 1 root root 10936 2008-09-19 15:23 sql_state.h
-rw-r--r-- 1 root root 1006 2008-09-19 15:23 sslopt-case.h
-rw-r--r-- 1 root root 2163 2008-09-19 15:23 sslopt-longopts.h
-rw-r--r-- 1 root root 1107 2008-09-19 15:23 sslopt-vars.h
-rw-r--r-- 1 root root 1300 2008-09-19 15:23 typelib.h
emanuele@emanuele-desktop:~$ ls -l /usr/lib/mysql
totale 14312
-rw-r--r-- 1 root root 1318 2008-09-19 15:24 libdbug.a
-rw-r--r-- 1 root root 54504 2008-09-19 15:24 libheap.a
-rw-r--r-- 1 root root 407864 2008-09-19 15:24 libmyisam.a
-rw-r--r-- 1 root root 33178 2008-09-19 15:24 libmyisammrg.a
-rw-r--r-- 1 root root 10209820 2008-09-19 15:24 libmysqld.a
-rw-r--r-- 1 root root 1365932 2008-09-19 15:24 libmystrings.a
-rw-r--r-- 1 root root 351496 2008-09-19 15:24 libmysys.a
-rw-r--r-- 1 root root 1398128 2008-09-19 15:24 libndbclient.a
-rw-r--r-- 1 root root 1245 2008-09-19 15:23 libndbclient.la
lrwxrwxrwx 1 root root 21 2009-03-15 21:57 libndbclient.so -> libndbclient.so.2.0.0
lrwxrwxrwx 1 root root 21 2009-03-15 21:57 libndbclient.so.2 -> libndbclient.so.2.0.0
-rw-r--r-- 1 root root 751588 2008-09-19 15:24 libndbclient.so.2.0.0
-rw-r--r-- 1 root root 15316 2008-09-19 15:24 libvio.a

Will I istall mysql_devel? :( i use ubuntu 8.10

Sorry for late reply ,

Do one thing,

gcc -I/usr/include/mysqltotale -o FOORUN $(mysql_config --cflags) foo.c $(mysql_config --libs)

and make sure u have included mysql.h file in following fashion
#include<mysql/mysql.h> in your source file as foo.c

If thats not working , install mysql_devel and then try... you can download mysql_devel for Debian ..

Hi I got the same problem when I link mysql library in makefile. How did you solve it ? Can you show me your makefile?

in this code i have found error like :

Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\Dev-Cpp\db.c" -o "C:\Dev-Cpp\db.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
In file included from C:/Dev-Cpp/include/mysql.h:46,
from C:\Dev-Cpp\db.c:1:

C:/Dev-Cpp/include/mysql_com.h:84: error: syntax error before "HANDLE"

C:/Dev-Cpp/include/mysql_com.h:84: warning: no semicolon at end of struct or union

C:/Dev-Cpp/include/mysql_com.h:86: warning: data definition has no type or storage class

C:/Dev-Cpp/include/mysql_com.h:96: error: syntax error before '}' token
C:/Dev-Cpp/include/mysql_com.h:96: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql_com.h:126: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql_com.h:128: error: syntax error before '*' token

C:/Dev-Cpp/include/mysql_com.h:129: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql_com.h:130: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql_com.h:131: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql_com.h:132: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql_com.h:134: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql_com.h:135: error: syntax error before '*' token
In file included from C:/Dev-Cpp/include/mysql.h:46,
from C:\Dev-Cpp\db.c:1:
C:/Dev-Cpp/include/mysql_com.h:201:7: warning: no newline at end of file
In file included from C:/Dev-Cpp/include/mysql.h:47,
from C:\Dev-Cpp\db.c:1:
C:/Dev-Cpp/include/mysql_version.h:10:33: warning: no newline at end of file
In file included from C:\Dev-Cpp\db.c:1:
C:/Dev-Cpp/include/mysql.h:111: error: syntax error before "NET"

C:/Dev-Cpp/include/mysql.h:111: warning: no semicolon at end of struct or union
C:/Dev-Cpp/include/mysql.h:128: error: syntax error before '}' token
C:/Dev-Cpp/include/mysql.h:128: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:141: error: syntax error before "MYSQL"
C:/Dev-Cpp/include/mysql.h:141: warning: no semicolon at end of struct or union
C:/Dev-Cpp/include/mysql.h:143: error: syntax error before '}' token
C:/Dev-Cpp/include/mysql.h:143: warning: data definition has no type or storage class

C:/Dev-Cpp/include/mysql.h:163: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:163: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:163: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:164: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:164: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:165: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:167: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:167: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:173: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:182: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:183: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:184: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:185: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:187: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:188: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:189: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:190: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:191: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:193: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:194: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:195: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:196: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:198: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:199: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:200: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:200: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:200: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:201: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:201: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:201: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:202: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:202: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:203: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:204: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:204: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:204: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:205: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:205: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:205: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:206: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:206: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:206: warning: data definition has no type or storage class
C:/Dev-Cpp/include/mysql.h:207: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:209: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:210: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:211: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:212: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:214: error: syntax error before '*' token
C:/Dev-Cpp/include/mysql.h:215: error: syntax error before '*' token

C:/Dev-Cpp/include/mysql.h:216: error: syntax error before '*' token
In file included from C:\Dev-Cpp\db.c:1:

C:/Dev-Cpp/include/mysql.h:228:7: warning: no newline at end of file
C:\Dev-Cpp\db.c: In function `main':
C:\Dev-Cpp\db.c:6: error: syntax error before "mysql"
C:\Dev-Cpp\db.c:8: error: `result' undeclared (first use in this function)
C:\Dev-Cpp\db.c:8: error: (Each undeclared identifier is reported only once
C:\Dev-Cpp\db.c:8: error: for each function it appears in.)
C:\Dev-Cpp\db.c:13: error: `mysql' undeclared (first use in this function)

Execution terminated

The two variables work fine at the sh prompt found empty if used in the makefile. But I was so sly :) and I displayed them by typing
echo $(mysql_config --cflags)
and
echo $(mysql_config --libs)
Then I copied the results directly to the makefile. Don't forget to do so anytime you made changes to your MySQL client environment.

It was tested on a Raspberry Pi model B with Debian (raspbian) linux on it.

# ---------------------------------------------------------------------------
# This is a sample makefile
# http://www.daniweb.com/software-development/c/threads/180705/how-to-include-mysql.h-in-makefile
# $(mysql_config --cflags) and $(mysql_config --libs) are somewhy empty but
# I displayed them by echo command at the sh prompt then cntl-c - cntl-v'd
# to this file. :)
# ---------------------------------------------------------------------------

CC= gcc
MYSQLCFLAGS= -I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g
MYSQLLIBS= -L/usr/lib/arm-linux-gnueabihf -lmysqlclient -lpthread -lz -lm -lrt -ldl

sql: sql.c
    $(CC) sql.c -o sql $(MYSQLCFLAGS) $(MYSQLLIBS)
    chmod 555 sql

clean:
    rm -f sql
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.