Error linking to MySQL

Hello, good afternoon, I'm making a C++ program with CodeBlocks (20.03 - MinGW - Windows 10 x64) that connects to a MySQL database but the linker is failing me and I don't understand it...

I use libmysql.a and:

- I have it referenced in Project build options > Linker settings ("C:\Users\Pablo\Documents\C++\OrganizadorProductosPAP\libmysql.a").

- Then in Project build options > Search directories > Compiler I have the include folder of the project ("C:\Users\Pablo\Documents\C++\ OrganizerProductsPAP\include").

When building I get the following errors:

C:\Users\Pablo\Documents\C++\OrganizadorProductosPAP\main.cpp|28|undefined reference to `mysql_init'|

C:\Users\Pablo\Documents\C++\OrganizadorProductosPAP\main.cpp|29|undefined reference to `mysql_real_connect'|

The code:

#include <mysql.h>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>

using namespace std;

const char* hostname = "localhost";
const char* username = "root";
const char* password = "";
const char* database = "productos";
unsigned int port = 3306;
const char* unixsocket = NULL;
unsigned long clientflag = 0;

connectDatabase(){
MYSQL* conn;
conn = mysql_init(0);
conn = mysql_real_connect(conn, hostname, username, password, database, port, unixsocket, clientflag);
if (conn){
cout << "Conexión realizada con éxito!!!" << endl;
}else{
cout << "Ha habido un error..." << endl;
}
}

int main()
{
connectDatabase();
}
What *.a -files are in C:\Users\Pablo\Documents\C++\OrganizadorProductosPAP\ ?
If you are trying to use the static library, try adding this to compiler options:
-DSTATIC_CONCPP

Also be sure that the .a file you are trying to link matches the target architecture!
Last edited on
@keskiverto I linked in these directory with libmysql.a, If I'm not mistaken, I had been recommended to link it with libmysqlclient.a but I didn't find that file anywhere and I think it's for Unix systems
@kigar64551 I put this command in Project build options > Debug > Compiler Settings > Other Compiler Options but not works :/
I'm not using Code::Blocks IDE, but building from the MSYS2/Mingw-w64 shell directly.

A minimal program like this builds just fine for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* test.c */

#include <mysql.h>
#include <stdlib.h>
#include <stdio.h>

const char* hostname = "localhost";
const char* username = "root";
const char* password = "root";
const char* database = "test";

static MYSQL *connectDatabase()
{
    MYSQL* conn;
    if (!(conn = mysql_init(NULL)))
    {
        puts("Initialization has failed!");
        return NULL;
    }
    if (conn = mysql_real_connect(conn, hostname, username, password, database, 3306, NULL, 0))
    {
        puts("Success.");
    }
    else
    {
        puts("Failed to connect!");
    }
    return conn;
}

int main()
{
    connectDatabase();
}


Command used to build was:
1
2
$ gcc -I'/c/Program Files/MySQL/MySQL Server 8.0/include' \
  -L'/c/Program Files/MySQL/MySQL Server 8.0/lib' test.c -lmysql


I think -DSTATIC_CONCPP is for the MySQL Connector/C++, not for the "native" MySQL C API.

(I'm using the latter in my example, as you apparently were too)
Last edited on
@kigar64551 I understand what you mean but in my case I am a beginner with it and I use XAMPP instead of MySQL Server itself. Could you help me in what I tell you please? :/
XAMPP is really just a kind of "all in one" package with Apache + PHP + Perl + MySQL Server.

I think it will be the simplest to just use the "official" MySQL installer:
https://dev.mysql.com/downloads/installer/

Once you have installed MySQL server, the required libraries/header files for the C API will be available at:
1
2
C:\Program Files\MySQL\MySQL Server 8.0\lib
C:\Program Files\MySQL\MySQL Server 8.0\include


I already gave you a full example how to use those, if you call the compiler from the command-line. It should be straight-forward to reproduce those commands in an IDE, e.g. Code::Blocks, but I haven't tried that...

Just for the notes, the MySQL C++ API, aka "Connector/C++" is a separate download:
https://dev.mysql.com/downloads/connector/cpp/8.0.html
Last edited on
Sorry I'm busy these days, I finally installed the entire MySQL and put the MySQL Server directories in Search Directories together with the program folder and the Connector directory, it still doesn't go...
@kigar64551 I was able to link to the official MySQL libmysql.lib that I found in the MySQL directories but when starting the application I get: The application was unable to start correctly (0xc000007b) :/
Thank you very much everyone, I saw that libmysql.dll was missing in the folder and I copied it from MySQL and it just connected the database, I'm very happy, greetings!
Topic archived. No new replies allowed.