0

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?

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – S.S. Anne Oct 11 '19 at 23:04
  • 1
    I am not using visual studio, but thanks for the link, I'm reading it through and checking them. I'll mark as answered if an answer is found in there, again though thanks for answering! – He Who Hid His Name For Peace Oct 11 '19 at 23:09
  • One more relevant link: https://stackoverflow.com/questions/30130631/error-while-connecting-c-program-to-mysql-database. See also the duplicate to this. – S.S. Anne Oct 11 '19 at 23:13
  • Are you by chance using Code::Blocks? – S.S. Anne Oct 11 '19 at 23:14
  • 1
    Ah, no I started learning by "Learn C the hard way", and the author gave the good advice of 'avoid using IDEs to build-up muscle memory' or something along the lines. Anyways, I'm just using Notepad++ and Cygwin. – He Who Hid His Name For Peace Oct 11 '19 at 23:21

1 Answers1

0

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.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76