Results 1 to 7 of 7

Thread: GCC compiling error related to MySQL header file and references in the header file

  1. #1
    Join Date
    Jun 2014
    Beans
    4

    GCC compiling error related to MySQL header file and references in the header file

    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:

    Code:
    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:

    Code:
    #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;
    }
    
    

    Last edited by steeldriver; June 6th, 2014 at 02:21 PM. Reason: added [CODE] ... [/CODE] tags

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: GCC compiling error related to MySQL header file and references in the header fil

    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

    Code:
    #include <mysql/mysql.h>
    .
    .
    .
    and then to compile and link the actual library

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

    Hope this helps.

  3. #3
    Join Date
    Jun 2014
    Beans
    4

    Re: GCC compiling error related to MySQL header file and references in the header fil

    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.


  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: GCC compiling error related to MySQL header file and references in the header fil

    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.

    Code:
    mysql_real_connect(mysql, NULL,"root","my_mysql_root_pw", "mysql", 0,NULL,0);
    
    mysql_query(mysql, "SELECT Host,User FROM user");
    then
    Code:
    $ 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
    Last edited by steeldriver; June 6th, 2014 at 04:32 PM.

  5. #5
    Join Date
    Jun 2014
    Beans
    4

    Re: GCC compiling error related to MySQL header file and references in the header fil

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

  6. #6
    Join Date
    Apr 2012
    Beans
    7,256

    Re: GCC compiling error related to MySQL header file and references in the header fil

    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?

  7. #7
    Join Date
    Jun 2014
    Beans
    4

    Re: GCC compiling error related to MySQL header file and references in the header fil

    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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •