MySQL "undefined reference" problem with C

Discussion in 'Linux Ubuntu' started by acsabir, May 2, 2014.

  1. acsabir

    acsabir New Member

    Joined:
    Mar 28, 2014
    Messages:
    3
    Likes Received:
    0
    Hi,

    We have been developing a software (as a part of our senior project) that reads sensor values that Arduino puts on the serial line(ttymxc3) and stores them into the local MySQL server. MySQL is up and running normally, Arduino is up and running normally, and finally, my program can read serial line normally. It was fine until this moment.

    Then I wanted to implement MySQL functionality. I have installed mysql libraries, I have verified that they are sitting pretty under "/usr/include/mysql".

    Here is my sample code I use to test basic connect, query, close path. (I am using code::blocks)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    // #include "mysql.h"
    #include <mysql/mysql.h>
    
    
    MYSQL *mysql;
    MYSQL_RES *results;
    MYSQL_ROW record;
    
    static char *server_options[] = {"mysql_test", "--defaults-file=my.cnf"};
    int num_elements = sizeof(server_options) / sizeof(char *);
    
    static char *server_groups[] = {"libmysqd_server", "libmysqd_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, "hostnameHere","userNameHere","userPasswordHere", "bookstore", 0,"PortNumberHere",0);
       mysql_options(mysql, MYSQL_SET_CLIENT_IP, "10.1.1.2");
    
       mysql_query(mysql, "insertSQLstatementHere");
    
       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;
    }
    
    When I hit F9(build + run) I get this error:

    and same error about all the function calls I make. IDE sees all of them, and makes inline suggestions as I type. But fails at compile time.

    Note:
    Code:
    #define <mysql.h> 
    does not work, therefore I use
    Code:
    #define <mysql/mysql.h>
    What is my problem? And how do I fix it?
     
  2. acsabir

    acsabir New Member

    Joined:
    Mar 28, 2014
    Messages:
    3
    Likes Received:
    0
    Fixed it.

    Turns out it was a problem of passing correct parameters to the GCC compiler. Used this:

    Code:
    gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)
     

Share This Page