我已经使用 Windows 官方 MySQL 安装程序安装了 MySQL C Connector,但是与 GCC 链接后,它仍然抛出 undefined reference 错误。
我尝试重新安装并安装不同版本(即 6.0 到 6.1.1 的所有版本)。我尝试将所有“\”更改为“/”,我尝试给出一个错误的名称,然后继续抛出“找不到库”错误,所以我确信我给出了正确的路径。
GCC 命令:
gcc mysql_test.c -Wall -o "project_path\target\debug\mysql_test.exe" -I"C:\Program Files\MySQL\MySQL Connector C 6.1\include" -L"C:\Program Files\MySQL\MySQL Connector C 6.1\lib" -lmysql
抛出
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0x23): undefined reference to `mysql_init@4'
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0x44): undefined reference to `mysql_options@12'
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0x8d): undefined reference to `mysql_real_connect@32'
d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\UserPC\AppData\Local\Temp\cc479zw2.o:main.c:(.text+0xa2): undefined reference to `mysql_error@4'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:12: all] Error 1
我被引导相信这是链接错误。
这是我直接从文档复制的代码:
#include <stdio.h>
#include <mysql.h>
int main() {
MYSQL db;
mysql_init(&db);
mysql_options(&db,MYSQL_READ_DEFAULT_GROUP,"prj_name");
if (!mysql_real_connect(&db,"i","correctly","set","these",0,NULL,0)) {
fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&db));
}
return 0;
}
我只使用(学习)C 一个月左右,这是我第一次需要链接到库。
(预期结果是不会抛出错误并且编译成功。)
如何解决这些链接错误?
最佳答案
这个答案最终变得荒谬,并且对于我的具体案例来说绝对是小众的。事实证明,我不仅安装了 Mingw32 的实例,而且还在 Cygwin 之前指向了路径,因此它获得了完全优先级。 一旦我摆脱它,因为其他一切都是 Cygwin,链接就起作用了!我知道,这对所有抱有希望的网友来说都是彻底的失望,但是我能做什么呢? 您的问题的其他可能解决方案:
- 最佳实践是将源文件放在 GCC 中
- 确保没有安装冲突(我的情况)
- 如果您手动安装,请确保您的系统确实适合二进制文件(以检查是否为 32 位而不是 64 位为例)
- 最后,使用 Linux,因为显然这些人使用 apt-get 和 yum 很容易
非常感谢 Shadow 修复了我可怕的标签。
关于c - Windows x64 上的 MySQL C Connector 与 GCC 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58349286/