【发布时间】:2020-11-15 16:54:13
【问题描述】:
我正在尝试查看如何在 C 程序中嵌入 sql 代码,但是我在编译此代码时遇到了一个我无法理解的问题:
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
MYSQL *mysql;
MYSQL_RES *results;
MYSQL_ROW record;
int main() {
mysql = mysql_init(NULL);
if (mysql == NULL) {
fprintf(stderr, "%s\n", mysql_error(mysql));
return 1;
}
if (mysql_real_connect(mysql, "localhost", "root", "PassWord",
NULL, 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(mysql));
mysql_close(mysql);
return 1;
}
mysql_query(mysql, "SHOW DATABASES");
return 0;
}
这是我编译时编译器告诉我的:
clang++ -g -c testSql.cc
clang++ testSql.o -o testSql
/usr/bin/ld: testSql.o: in function `main':
/home/antoine/Documenti/L3/Information Management II/code/testSql.cc:12: undefined reference to `mysql_init'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:15: undefined reference to `mysql_error'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:19: undefined reference to `mysql_real_connect'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:21: undefined reference to `mysql_error'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:22: undefined reference to `mysql_close'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:26: undefined reference to `mysql_query'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:25: testSql] Errore 1
我已经检查了mysql.h文件,这些功能都实现了,所以我不明白为什么我会出现这个“未定义的引用”错误,有人知道这个错误的根源吗?
【问题讨论】:
-
你需要告诉编译器带有MySQL预编译代码的文件在哪里,比如
clang++ ... -lmysql;编译器将(希望)将其解释为在库的预定义位置中“查找 libmysql.so” -
在编译时通过包含 mysql.h 来提供函数的定义。然后,您必须在链接时提供 mysql 库以提供代码。
-
当我搜索“未定义的 mysql_init 引用”时,我找到了很多答案。这些都没有帮助吗?
-
这可能会有所帮助:stackoverflow.com/q/10970356/6699433
标签: mysql c embedded-sql