Search code examples
cgcclinkermysql-connector-c

MySQL C Connector linking error with GCC on Windows x64


I have installed MySQL C Connector with the official MySQL installer for windows, however after linking with GCC, it still throws undefined reference errors.

I tried reinstalling and installing different versions ( namely everything 6.0 through 6.1.1 ). I tried changing all the '\' to '/', I tried giving a bad name, which then proceeded to throw me a 'lib not found' error, so I'm sure I'm giving the right path.

The GCC Command:

gcc mysql_test.c -Wall -o "project_path\target\debug\mysql_test.exe" -I"C:\Program Files\MySQL\MySQL Connector C 6.1\include" -L"C:\Program Files\MySQL\MySQL Connector C 6.1\lib" -lmysql

which throws

d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0x23): undefined reference to `mysql_init@4'
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0x44): undefined reference to `mysql_options@12'
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0x8d): undefined reference to `mysql_real_connect@32'
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0xa2): undefined reference to `mysql_error@4'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:12: all] Error 1

which I have been led to believe are linking errors.

This is the code I copied directly from the documentation:

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


int main() {
    MYSQL db;

    mysql_init(&db);
    mysql_options(&db,MYSQL_READ_DEFAULT_GROUP,"prj_name");

    if (!mysql_real_connect(&db,"i","correctly","set","these",0,NULL,0)) { 
        fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&db)); 
    }
    return 0;
}

I have only been using (learning) C for a month or so, and this is the first time I needed to link in a library.

(expected results would be no thrown errors and a successful compilation.)

How do I resolve these linking errors?


Solution

  • The answer ended up being ridiculous and absolutely niché to my specific case. Turns out, I not only had an instance of Mingw32 installed, but also pointed to in path, BEFORE Cygwin, and thus it got complete priority. As soon as I got rid of it, since everything else was Cygwin, the linking worked! I know, this is a complete disappointment to any netizen who got hopeful, but what can I do? Other possible solutions to your problem:

    • It's best practice to put source files first in GCC
    • Make sure there are no conflicting installs (my case)
    • Make sure, if you installed manually, that your system is indeed fit for the binary (check if 32bit instead of 64bit as an example)
    • And lastly, use linux because apparently those guys have it easy with apt-get and yum

    Big thanks to Shadow who fixed my horrendous tags.