Undefined reference for mysql_query in simple C program

63 Views Asked by At

I started writing a simple C program that connects to MySQL. I included the file using #include <mysql/mysql.h>. The websites I had a look at all said just #include <mysql.h> but it does not exist for me. VS Code tells me when hovering over the include that it provides the desired methods, it does not show me an error during the code editing, and when I follow the link from include then it shows me the full file without any issues and with all the methods. But when I compile it all MySQL related methods show and undefined reference error.

Does anyone have any idea what is going wrong? Thank you in advance.

My code: (Definitely not completed yet but I want to fix this first)

#include "cJSON.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <mysql/mysql.h>

const char DIRECTORY[] = "/home/fabrice/Egos/";
const char DIRECTORY_CONFIG[] = "/home/fabrice/Egos/configs/";
const char VERSION[] = "a1";
enum VersionEnum { DEVELOPMENT, TEST, ACTIVE };
const enum VersionEnum VERSION_KIND = DEVELOPMENT;
static MYSQL *mySQL;

char* combineStrings(const char* a, char* b) {
    char* ret = malloc(strlen(a)+strlen(b)+1);
    strcpy(ret, a);
    strcat(ret, b);
    return ret;
}

void copyFileData(const char* from, const char* to) {
    FILE *a, *b;
    a = fopen(from, "r");
    b = fopen(to, "w");
    if(a == NULL || b == NULL) {
        perror("An error occured while trying to copy files\n");
        return;
    }
    char c;
    c = fgetc(a);
    while(c != EOF) {
        fputc(c, b);
        c = fgetc(a);
    }
    fclose(a);
    fclose(b);
}

/*Target directory needs to be finished with /*/
void loadConfig(char* name) {
    char* fileLocation = combineStrings(DIRECTORY_CONFIG, name);
    if(access(fileLocation, F_OK) == 0) {
        free(fileLocation);
        return;
    }
    mkdir(DIRECTORY_CONFIG, S_IRWXU);
    FILE *target = fopen(fileLocation, "w");
    fclose(target);
    copyFileData(combineStrings("configs/", name), fileLocation);
    free(fileLocation);
}

cJSON* getConfig(char *name, int bufferSize) {
    char buffer[bufferSize];
    FILE *file = fopen(combineStrings(DIRECTORY_CONFIG, name), "r");
    if(file == NULL)
        loadConfig(name);
    fread(buffer, 1, bufferSize, file);
    fclose(file);
    cJSON *json = cJSON_Parse(buffer);
    if(json == NULL) {
        perror("An error occured while trying to cast a text to json");
        cJSON_Delete(json);
        return NULL;
    }
    return json;
}
/*Will start a connection to the MySQL server and place it in static variable mySQL.
Commands can be given via mysql_query.
Can be closed via mysql_close*/
void startMySQL() {
    mySQL = mysql_init(NULL);
    if(mySQL == NULL) {
        perror("An error occured while trying to establish a connection to SQL\n");
        return;
    }
    cJSON *config = getConfig("MySQL.json", 1024);
    if(mysql_real_connect(mySQL, 
    cJSON_GetObjectItem(config, "host")->valuestring, 
    cJSON_GetObjectItem(config, "user")->valuestring, 
    cJSON_GetObjectItem(config, "password")->valuestring,
    NULL,
    cJSON_GetObjectItem(config, "port")->valueint,
    NULL, 0) == NULL) {
        perror("An error occured while trying to connect to SQL\n");
        mysql_close(mySQL);
    }
    free(config);
}

char* randomID(int length) {
    char* id = malloc(sizeof(int)*length+5);
    for(int i = 0; i<length; i++)
        sprintf(&id[i], "%d", rand()%10);
    return id;
}

void initilizeSQL() {
    if(mySQL == NULL)
        return;
    mysql_query(mySQL, "CREATE DATABASE IF NOT EXISTS users");
    mysql_query(mySQL, "");
    mysql_query(mySQL, "CREATE DATABASE IF NOT EXISTS roster");
    mysql_query(mySQL, "CREATE DATABASE IF NOT EXISTS sales");
    mysql_query(mySQL, "CREATE DATABASE IF NOT EXISTS devices");
}

int main() {
    printf("Loading Egos Production Server\n");
    switch (VERSION_KIND) {
    case DEVELOPMENT: printf("Version Development %s\n", VERSION); break;
    case TEST: printf("Version Test %s\n", VERSION); break;
    case ACTIVE: printf("Version %s\n", VERSION); break;
    }
    srand(time(NULL));
    loadConfig("MainFile.json");
    printf("Random: %s", randomID(8));
    return 0;
}

The errors I get: (Excluding the ones related to the unfinished code)

Executing task: /usr/bin/bash -c gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -g3 -O0 -c /home/fabrice/Workspace/Egos/backend/production-server/cJSON.c -o ./build/Debug/cJSON.o && gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -g3 -O0  -c /home/fabrice/Workspace/Egos/backend/production-server/main.c -o ./build/Debug/main.o && gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -g3 -O0   ./build/Debug/cJSON.o ./build/Debug/main.o -o ./build/Debug/outDebug 

/usr/bin/ld: ./build/Debug/main.o: in function `startMySQL':
/home/fabrice/Workspace/Egos/backend/production-server/main.c:76:(.text+0x313): undefined reference to `mysql_init'
/usr/bin/ld: /home/fabrice/Workspace/Egos/backend/production-server/main.c:82:(.text+0x3de): undefined reference to `mysql_real_connect'
/usr/bin/ld: /home/fabrice/Workspace/Egos/backend/production-server/main.c:90:(.text+0x405): undefined reference to `mysql_close'
/usr/bin/ld: ./build/Debug/main.o: in function `initilizeSQL':
/home/fabrice/Workspace/Egos/backend/production-server/main.c:105:(.text+0x4d6): undefined reference to `mysql_query'
/usr/bin/ld: /home/fabrice/Workspace/Egos/backend/production-server/main.c:106:(.text+0x4ef): undefined reference to `mysql_query'
/usr/bin/ld: /home/fabrice/Workspace/Egos/backend/production-server/main.c:107:(.text+0x508): undefined reference to `mysql_query'
/usr/bin/ld: /home/fabrice/Workspace/Egos/backend/production-server/main.c:108:(.text+0x521): undefined reference to `mysql_query'
/usr/bin/ld: /home/fabrice/Workspace/Egos/backend/production-server/main.c:109:(.text+0x53a): undefined reference to `mysql_query'
collect2: error: ld returned 1 exit status

I had seen online that using the command 'mysql_config --include' you can see where it is located, the response I get is '-I/usr/include/mysql -I/usr/include/mysql/mysql' which is correct according to the website.

1

There are 1 best solutions below

1
KamilCuk On

Does anyone have any idea what is going wrong?

You do not link with mysql.

I had seen online that using the command 'mysql_config --include'

Then except including the include files search path you have to also link with mysql library on linking stage. On my system:

$ mysql_config --libs
-L/usr/lib/ -lmariadb