使用g ++的MySQL库的未定义引用 [英] Undefined reference to MySQL libraries using g++

查看:142
本文介绍了使用g ++的MySQL库的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将我的程序与5.5服务器提供的MySQL库链接时,我得到未定义的引用mysql_suchandsuch @#当MySQL安装,我使用默认路径,对于我在Windows上是 C:\Program Files\MySQL\MySQL Server 5.5 \ 。最初,我认为空间正在导致我的悲伤,但我想我已经正确地计划如何指向图书馆的路径没有空格(仍然没有运气)。如果有其他可能的原因,请让我知道。



我已经审查了这个网站上的一系列问题,试图解决我的问题...





使用mingw / g ++,我尝试使用以下链接选项,根据我自己的研究以及建议:




  • -LC:\Program Files\MySQL\MySQL服务器5.5 \lib \-llibmysql.lib

  • -LC:\Program Files\MySQL\MySQL Server 5.5 \lib \-lmysqlclient.lib

  • -LC:\Progra〜1 \MySQL\MySQLS〜1.5 \lib\-llibmysql.lib

  • -LC:\Progra〜1 \MySQL\MySQLS〜1.5\lib\ -lmysqlclient.lib

  • -LC:\Progra〜1 \MySQL\\ \\ MySQLS〜1.5 \lib \-lmysql



在所有情况下,我都把 L / -l <​​/ code>选项,因为我知道这可能很重要。



我已经确认库存在。在/ lib目录中,我有libmysql.lib,mysqlclient.lib和libmysql.dll。我没有尝试链接到.dll,因为没有教程/论坛我已经审查建议。



我不使用MAKEFILES。 >



有谁有g ++ / MySQL的特定经验?

解决方案

以下命令对我使用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



因此,与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]

这篇关于使用g ++的MySQL库的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆