Search code examples
mysqlclibrarieswamp64

mysql_init(NULL) crashes my C program using a wamp64 server


I'm working on a school project in C language, during which I need to make a connection with a local database. For this, I'm using a wamp64 local server. My problem, however, is entirely outside of that to my comprehension. The compiler SEEMS to detect and properly import the necessary library, this being I believe the MYSQL C API, however it crashes once I use the mysql = mysql_init(NULL) command, which is a bit problematic. Everytime the result is the same:

Process finished with exit code -1073741515 (0xC0000135) Here is the code of the file main.c, where I'm running my test commands: (Yes, I am using GTK as well if that causes an issue)


#include <stdio.h>
#include <winsock.h>
#include <MYSQL/mysql.h>
#include <gtk-4.0/gtk/gtk.h>
#include <stdlib.h>
#include <string.h>

#include "createMenu.h"
#include "databaseMenu.h"

static void activate(GtkApplication* app, gpointer user_data);
int main (int argc, char **argv);

static void activate (GtkApplication* app, gpointer user_data)
{
    GtkWindow *window;

    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Database C Editor");
    gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

    GtkWidget* content;
    content = prepareDataBaseScreen();

    gtk_window_set_child(window, content);


    gtk_widget_show (window);
}

int main (int argc, char **argv)
{
    GtkApplication *app;
    int status;

    MYSQL * mysql;
    mysql = mysql_init(NULL); // Erreur ici
    mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "option");

    if (mysql_real_connect(mysql, "localhost", "root", "", NULL, 0, NULL, 0))
    {
        g_print("Success");
    } else {
        g_print("Failure");
    }
    mysql_close(mysql);

    app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return status;
}

I doubt it comes from anywhere except the main function but just in case I put the entire code in there. Now since that might be linked as well, I want to give you the CMakeLists.txt of my project, where I'm importing both libraries I'm using:

cmake_minimum_required(VERSION 3.18)
project(ProjectCESGI LANGUAGES C)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtk4)
include_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/include")
link_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/lib")
link_libraries(libmariadb)

add_executable(ProjectCESGI main.c createMenu.h databaseMenu.h columnWidget.h)
target_link_libraries(ProjectCESGI PRIVATE PkgConfig::GTK4 libmariadb)

Finally, my teacher gave us a sample code in which he uses the same library. Alarmingly, this one actually works on my machine. Here's his main.c:

#include <stdio.h>
#include <winsock.h>
#include <MYSQL/mysql.h>
#include <stdlib.h>

int main() {

    MYSQL *mysql;
    mysql = mysql_init(NULL);
    mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "option");

    if (mysql_real_connect(mysql, "localhost", "root", "", NULL, 0, NULL, 0)) {
        printf("MySQL client version: %s\n", mysql_get_client_info());
        mysql_query(mysql, "CREATE DATABASE IF NOT EXISTS dbexample");
        mysql_query(mysql, "USE dbexample");
        mysql_query(mysql, "DROP TABLE IF EXISTS students");
        mysql_query(mysql, "CREATE TABLE students(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), class INT)");
        mysql_query(mysql, "INSERT INTO students VALUES(1,'Mathieu',1)");
        mysql_query(mysql, "INSERT INTO students VALUES(2,'Ryan',12)");
        mysql_close(mysql);
    } else {
        printf("Erreur connexion à la BDD!");
    }
    return EXIT_SUCCESS;
}

If you need anything else, please do tell as I need this problem fixed and I've gotten kinda desperate. Thank you for any answer!

EDIT: Someone requested that I put my teacher's CMakeLists.txt as well so here it is:

cmake_minimum_required(VERSION 3.12)
project(test C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "-Wall")
include_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/include")
link_directories("C:/wamp64/bin/mariadb/mariadb10.6.5/lib")
link_libraries(libmariadb)
add_executable(test main.c)
target_link_libraries(test libmariadb)

# les fichiers libmariadb.dll et libmariadb.lib sont copiés de
# C:\wamp64\bin\mariadb\mariadb10.4.10\lib
# dans le dossier de compilation
# C:\Users\fredguy\CLionProjects\test\cmake-build-debug\

Do notice that I've changed every mention of "mariadb10.4.10" to "mariadb10.6.5" as that's the version I have installed. Keep in mind it's working just fine on his exemple project.

EDIT2: After someone pointed out that this specific error code meant DLL files were missing, I investigated in my debug folder and indeed it seems the program doesn't copy the libmariadb.dll an libmysql.dll files, while of course it works on the teacher's exemple. Any idea why that is?

-Tracking down the bug using a few methods to see where exactly the program crashes, letting me know that mysql_init seems to be indeed the problem.

-Googling the problem; Only forums are on a MYSQL forum which seems to have never been answered

-Replacing 'NULL' with a few things, like '0' or 'mysql'.

-Making another project just to import it on that other project (Couldn't work either)


Solution

  • As it turns out, I just didn't understand how this actually works. The .dll files simply weren't in the file where CLion's debug functionnality compiled the program, all I had to do was copy and paste them in said file. Sorry for the inconvenience, hope this might help another confused student later.