Undefined reference to MySQL libraries using g++

2019-06-19 17:26发布

问题:

I am getting undefined reference to 'mysql_suchandsuch@#' messages when trying to link my program with the MySQL libraries supplied with the 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\. Originally, I had thought that the spaces are causing my grief, but I think I've correctly worked out how to point to the library path without spaces (still with no luck). If there's another probable cause, please let me know.

I have reviewed a series of questions on this site trying to resolve my issue...

  • Question 1
  • Question 2
  • Question 3
  • Question 4
  • Question 5
  • Question 6
  • Question 7

Using mingw/g++, I have tried to link using the following options, based on my own research as well as 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 have put the -L/-l options at the right-most part of the statement, as I understand this can matter.

I have confirmed the libraries do exist. In the /lib dir, I have libmysql.lib, mysqlclient.lib, and libmysql.dll. I have not tried to link with the .dll, as no tutorial/forum I've reviewed suggested that.

I am not using MAKEFILES.

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

回答1:

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

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

g++ my.cpp -I D:\Opt\MySQL5.5\include ^
  -L D:\Opt\MySQL5.5\lib -lmysql -o myWithLib.exe

So both linking against the LIB and the DLL do work.

You may get a warning (see Gaffi's comment). This is because the linker does fuzzy linking for you without you having it specified; normally, it would have failed to link. It is being nice, though, and making it work for you, at the same time warning you about things happening without your having requested them. The way to suppress the warning is to make fuzzy linking explicit:

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

g++ -Wl,--enable-stdcall-fixup my.cpp -I D:\Opt\MySQL5.5\include ^
  -L D:\Opt\MySQL5.5\lib -lmysql -o myWithLib.exe

This is a Cygwin/RedHat/MinGW extension to the linker; the docs are here:

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

If the link[er] finds a symbol that it cannot resolve, it will attempt to do “fuzzy linking” by looking for another defined symbol that differs only in the format of the symbol name (cdecl vs stdcall) and will resolve that symbol by linking to the match. For example, the undefined symbol _foo might be linked to the function _foo@12, or the undefined symbol _bar@16 might be linked to the function _bar. When the linker does this, it prints a warning, since it normally should have failed to link, but sometimes import libraries generated from third-party dlls may need this feature to be usable. If you specify --enable-stdcall-fixup, this feature is fully enabled and warnings are not printed. If you specify --disable-stdcall-fixup, this feature is disabled and such mismatches are considered to be errors. [This option is specific to the i386 PE targeted port of the linker]



回答2:

did you try -lmysql as the linker automatically prepends lib and appends .lib