gpt4 book ai didi

c++ - 使用 g++ 对 MySQL 库的 undefined reference

转载 作者:可可西里 更新时间:2023-11-01 06:51:35 25 4
gpt4 key购买 nike

当我尝试将我的程序与 5.5 服务器提供的 MySQL 库链接时,我收到了 undefined reference to 'mysql_suchandsuch@#' 消息。安装 MySQL 时,我使用默认路径,在 Windows 上对我来说是 C:\Program Files\MySQL\MySQL Server 5.5\。最初,我以为这些空格让我很伤心,但我认为我已经正确地弄清楚了如何在没有空格的情况下指向库路径(仍然没有运气)。如果还有其他可能的原因,请告诉我。

我已查看此网站上的一系列问题,试图解决我的问题...

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

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

在所有情况下,我都将 -L/-l 选项放在语句的最右侧,因为我知道这很重要。

我已经确认图书馆确实存在。在/lib 目录中,我有 libmysql.lib、mysqlclient.lib 和 libmysql.dll。我没有尝试与 .dll 链接,因为我看过的教程/论坛都没有建议这样做。

我没有使用 MAKEFILES。

有没有人有使用 g++/MySQL 的具体经验?

最佳答案

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

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

因此,针对 LIB 和 DLL 的链接都有效。

您可能会收到警告(请参阅 Gaffi 的评论)。这是因为链接器在您没有指定的情况下为您进行了模糊链接;通常,它将无法链接。不过,它很好,让它为你工作,同时警告你关于发生的事情而你没有要求它们。抑制警告的方法是使模糊链接显式:

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

这是链接器的 Cygwin/RedHat/MinGW 扩展; 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]

关于c++ - 使用 g++ 对 MySQL 库的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com