PDA

View Full Version : GCC compiling error related to MySQL header file and references in the header file



cercig
June 6th, 2014, 02:02 PM
Hi,

I need an urgent answer. I am trying to write a C code with embedded MySQL server and client options. The compiler is gcc.I added the location of the header file manually in the code; like: #include "/usr/include/mysql/mysql.h"

I have the necessary library installed: libmysqld-dev which includes MySQL embedded development libraries.

When I compile my code very simply like: "gcc test.c" I get errors like this:



prov.c:(.text+0x1b): undefined reference to `mysql_server_init'
prov.c:(.text+0x25): undefined reference to `mysql_init'
prov.c: (.text+0x45): undefined reference to `mysql_options'
prov.c: (.text+0x5e): undefined reference to `mysql_options'
prov.c: (.text+0x99): undefined reference to `mysql_real_connect'
prov.c: (.text+0xad): undefined reference to `mysql_query'
prov.c: (.text+0xbc): undefined reference to `mysql_store_result'
prov.c: (.text+0x101): undefined reference to `mysql_fetch_row'
prov.c: (.text+0x123): undefined reference to `mysql_free_result'
prov.c: (.text+0x132): undefined reference to `mysql_close'
prov.c: (.text+0x137): undefined reference to `mysql_server_end'


My code is below:



#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "/usr/include/mysql/mysql.h"

MYSQL *mysql;
MYSQL_RES *results;
MYSQL_ROW record;

static char *server_options[] = { "prov", "--defaults-file=my.cnf" };
int num_elements = sizeof(server_options)/ sizeof(char *);

static char *server_groups[] = { "libmysqld_server", "libmysqld_client" };

int main(void)
{
mysql_server_init(num_elements, server_options, server_groups);
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);


mysql_real_connect(mysql, NULL,NULL,NULL, "data", 0,NULL,0);

mysql_query(mysql, "SELECT data_id, from the data series");

results = mysql_store_result(mysql);

while((record = mysql_fetch_row(results))) {
printf("%s - %s \n", record[0], record[1]);
}

mysql_free_result(results);
mysql_close(mysql);
mysql_server_end();

return 0;
}

steeldriver
June 6th, 2014, 02:27 PM
Hello and welcome to the forums - I have added CODE tags to your post to make it easier to read - please use them when you post code or terminal output

The errors you are getting are from the link phase, rather than the compile phase - so they are related to missing libraries not missing header files. As far as I know the mysql.h header is provided by the libmysqclient-dev package and once it's installed you should be able to just do



#include <mysql/mysql.h>
.
.
.


and then to compile and link the actual library



gcc test.c -lmysqlclient


(I would add a -o test ans well, else the resulting executable gets built as a.out)

Hope this helps.

cercig
June 6th, 2014, 03:54 PM
Thanks steeldriver,

I am completely new, and I needed urgent answer. Thanks a lot for every helping.

It compiles without error now with -lmysqlclient

However, now I am getting:

"Segmentation fault (core dump)"

I guess maybe I didn't prescribe correctly for the data file "data.csv".

Can't my code open and read csv file directly? I saw on internet like:
LOAD DATA LOCAL INFILE 'data.csv' INTO TABLE test FIELDS TERMINATED BY ',' lines terminated by '\n';

However, I am not sure how to add this structure into my code.

Any (hopefully) last recommendation?

Thanks.

steeldriver
June 6th, 2014, 04:07 PM
I don't have any experience in mysql client programming - sorry

Hopefully someone else will be able to help, although you may have better luck on a mysql or developer forum (since this isn't really an Ubuntu specific question)

EDIT: FWIW you code actually runs for me if I replace the connection and query strings with something appropriate for my environment i.e.



mysql_real_connect(mysql, NULL,"root","my_mysql_root_pw", "mysql", 0,NULL,0);

mysql_query(mysql, "SELECT Host,User FROM user");

then


$ gcc -O0 -g -o test test.c -lmysqlclient
$ ./test
127.0.0.1 - root
::1 - root
lap-t61p -
lap-t61p - root
localhost -
localhost - debian-sys-maint
localhost - root

cercig
June 6th, 2014, 05:44 PM
Thanks again,

My main problem might be the CSV data file. How to desribe it in C and how to import it in the mysql line. I don NOT think it works if I only write "mysql.csv" as data file in the line below.

mysql_real_connect(mysql, NULL,"root","my_mysql_root_pw", "mysql", 0,NULL,0);

steeldriver
June 6th, 2014, 06:37 PM
What is your end goal exactly? Do you actually need to write a C program - or do you just need a way to import data from a CSV file into a mysql database?

cercig
June 6th, 2014, 07:05 PM
Importing that "data.csv" data to MySQL,
Then creating a server, and also a client with visual interface.
When I search a criteria through the client, the client will communicate with the server and the client will display the result.
And everything must be in C code :)