MySQL Windows x64 上的 GCC C 连接器链接错误
MySQL C Connector linking error with GCC on Windows x64
我已经使用 windows 的官方 MySQL 安装程序安装了 MySQL C 连接器,但是在 link 使用 GCC 后,它仍然会抛出未定义的引用错误。
我尝试重新安装和安装不同的版本(即 6.0 到 6.1.1 的所有版本)。我尝试将所有“\”更改为“/”,我尝试提供一个错误的名称,然后继续抛出一个 'lib not found' 错误,所以我确定我给出了正确的路径。
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
我被引导相信是 linking 错误。
这是我直接从文档中复制的代码:
#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 语言一个月左右,这是我第一次需要在图书馆link。
(预期结果是没有抛出错误并且编译成功。)
如何解决这些 linking 错误?
答案最终变得荒谬可笑,而且对我的具体情况来说绝对是小众的。事实证明,我不仅安装了一个 Mingw32 实例,而且还在路径中指向了 Cygwin 之前,因此它获得了完全优先级。
一旦我摆脱它,因为其他一切都是 Cygwin,链接就成功了!我知道,这对任何有希望的网友来说都是一个彻底的失望,但我能做什么呢?
您的问题的其他可能解决方案:
- 在 GCC 中最好将源文件放在前面
- 确保没有安装冲突(我的情况)
- 如果您手动安装,请确保您的系统确实适合二进制文件(例如检查 32 位而不是 64 位)
- 最后,使用 linux 因为显然那些人使用 apt-get 和 yum
很容易
非常感谢 Shadow 修复了我可怕的标签。
我已经使用 windows 的官方 MySQL 安装程序安装了 MySQL C 连接器,但是在 link 使用 GCC 后,它仍然会抛出未定义的引用错误。
我尝试重新安装和安装不同的版本(即 6.0 到 6.1.1 的所有版本)。我尝试将所有“\”更改为“/”,我尝试提供一个错误的名称,然后继续抛出一个 'lib not found' 错误,所以我确定我给出了正确的路径。
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
我被引导相信是 linking 错误。
这是我直接从文档中复制的代码:
#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 语言一个月左右,这是我第一次需要在图书馆link。
(预期结果是没有抛出错误并且编译成功。)
如何解决这些 linking 错误?
答案最终变得荒谬可笑,而且对我的具体情况来说绝对是小众的。事实证明,我不仅安装了一个 Mingw32 实例,而且还在路径中指向了 Cygwin 之前,因此它获得了完全优先级。 一旦我摆脱它,因为其他一切都是 Cygwin,链接就成功了!我知道,这对任何有希望的网友来说都是一个彻底的失望,但我能做什么呢? 您的问题的其他可能解决方案:
- 在 GCC 中最好将源文件放在前面
- 确保没有安装冲突(我的情况)
- 如果您手动安装,请确保您的系统确实适合二进制文件(例如检查 32 位而不是 64 位)
- 最后,使用 linux 因为显然那些人使用 apt-get 和 yum 很容易
非常感谢 Shadow 修复了我可怕的标签。