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