Undefined link to MySQL libraries using g ++

I get undefined reference to ' mysql_suchandsuch@ #' messages undefined reference to ' mysql_suchandsuch@ #' when I try to link my program with the MySQL libraries that ship with 5.5 server. When MySQL was installed, I used the default path, which for me on Windows is C:\Program Files\MySQL\MySQL Server 5.5\ . Initially, I thought that spaces cause my grief, but I think I correctly developed how to indicate the path to the library without spaces (anyway, no luck). If there is another plausible reason, please let me know.

I reviewed a number of issues on this site, trying to solve my problem ...

Using mingw / g ++, I tried to link using the following parameters based on my own research, as well as the suggestions here:

  • -L "C: \ Program Files \ MySQL \ MySQL Server 5.5 \ lib \" -llibmysql.lib
  • -L "C: \ Program Files \ MySQL \ MySQL Server 5.5 \ lib \" -lmysqlclient.lib
  • -L "C: \ Progra ~ 1 \ MySQL \ MySQLS ~ 1.5 \ lib \" -llibmysql.lib
  • -LC: \ Progra ~ 1 \ MySQL \ MySQLS ~ 1.5 \ lib \ -lmysqlclient.lib
  • -L "C: \ Progra ~ 1 \ MySQL \ MySQLS ~ 1.5 \ lib \" -lmysql

In all cases, I set the -L / -L options on the very right side of the instruction, as I understand it, it can make a difference.

I confirmed that libraries exist. In the / lib directory, I have libmysql.lib, mysqlclient.lib and libmysql.dll. I did not try to contact the .dll, since not a single tutorial / forum that I reviewed offered.

I DO NOT use MAKEFILES.

Does anyone have specific experience with g ++ / MySQL?

+6
source share
2 answers

The following commands work fine for me using GCC 4.6.1 since November 2011:

 g++ my.cpp -ID:\Opt\MySQL5.5\include ^ D:\Opt\MySQL5.5\lib\libmysql.dll -o myWithDll.exe g++ my.cpp -ID:\Opt\MySQL5.5\include ^ -LD:\Opt\MySQL5.5\lib -lmysql -o myWithLib.exe 

Thus, both references to LIB and DLL work.

You may receive a warning (see Guffy's comment). This is because the linker fuzzily binds you without specifying it; as a rule, he could not tie. It is nice, however, to make it work for you, while at the same time warning you about what happens if you did not ask them. A way to suppress a warning is to make fuzzy binding explicit:

 g++ -Wl,--enable-stdcall-fixup my.cpp -ID:\Opt\MySQL5.5\include ^ D:\Opt\MySQL5.5\lib\libmysql.dll -o myWithDll.exe g++ -Wl,--enable-stdcall-fixup my.cpp -ID:\Opt\MySQL5.5\include ^ -LD:\Opt\MySQL5.5\lib -lmysql -o myWithLib.exe 

This is the Cygwin / RedHat / MinGW extension for the linker; documents are here :

 --enable-stdcall-fixup --disable-stdcall-fixup 

If the link [er] finds a character that he cannot solve, he will try to make a โ€œfuzzy bindingโ€, looking for another specific character that differs only in the format of the character name (cdecl vs stdcall) and will resolve this character by associating it with a match . For example, the undefined symbol _foo may be associated with the function _foo @ 12 or the undefined symbol _foo @ 16 may be associated with the function _bar. when the linker does this, it prints a warning, because it usually should have failed to link, but sometimes you need to use this function to import libraries created from third-party DLLs. If you specify --enable-stdcall-fixup, this function is fully enabled and warns you are not printing. If you specify --disable-stdcall-fixup, this function is disabled, and such inconsistencies are considered errors. [This parameter is specific to the destination port of the i386 linker PE]

+1
source

tried -lmysql as linker automatically adds lib and adds .lib

0
source

Source: https://habr.com/ru/post/912996/


All Articles