mysql - mysql.h 编译,但不是它的功能
问题描述
我正在尝试查看如何在 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 文件,这些功能已实现,所以我不明白为什么我有这个“未定义的引用”错误,有人知道这个错误的根源吗?
解决方案
谢谢大家的回答。我必须在我的编译中提供一个链接器。我现在正在编译
clang++ -g -c testSql.cc
clang++ testSql.o -o testSql `mysql_config --cflags --libs`
代替
clang++ -g -c testSql.cc
clang++ testSql.o -o testSql
它编译正确。谢谢 !
推荐阅读
- vue.js - 探索性 Vue.js 示例所需的存储空间
- javascript - 更新卡片类别时按钮不起作用
- c# - Prism导航返回页面后打破页面绑定
- c - 我用什么代码来询问数量和付款?
- apache-spark - Spark - 读取和写入相同的 S3 位置
- mysql - 如何在 MySQL 中回滚到 django 默认创建的事务?
- java - 将超类方法返回的超类实例转换为子类实例
- php - 数据库播种 laravel 创建 50 个用户并为每个用户附加一个关系
- vhdl - Shift left register with load and enable signals
- apl - APL 中的重复函数应用