且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用g ++的MySQL库的未定义引用

更新时间:2023-01-25 14:09:31

以下命令对我使用2011年11月的GCC 4.6.1很好:

  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
$ p>

因此,与LIB和DLL的链接都可以工作。



警告(见Gaffi的评论)。这是因为链接器在没有指定的情况下为您模糊链接;通常,它将无法链接。这是很好的,虽然,使它为你工作,同时警告你的事情发生没有你要求他们。抑制警告的方法是使模糊连接显式:

  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

这是一个Cygwin / RedHat / MinGW扩展文档位于此处

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




如果链接找到一个无法解析的符号,它将尝试
通过查找另一个定义的符号,它仅在符号名称(cdecl vs stdcall)的格式中与
不同,并且
通过链接到匹配来解析该符号。例如,
未定义的符号_foo可能链接到函数_foo @ 12,或者
未定义的符号_bar @ 16可能链接到函数_bar。当
链接器这样做,它打印一个警告,因为它通常应该
链接失败,但有时从
生成的导入库第三方dll可能需要此功能可用。如果指定
--enable-stdcall-fixup,则此功能完全启用,并且不会打印警告
。如果指定--disable-stdcall-fixup,此功能将禁用
,并且这种不匹配被视为错误。 [此选项
特定于链接器的i386 PE目标端口]



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...

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?

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]