【发布时间】:2015-10-08 09:07:54
【问题描述】:
fcts_mysql.h:
MYSQL * cmd_mysql_init();
fcts_mysql.c:
#include "fcts_mysql.h"
MYSQL *cmd_mysql_init(){
return mysql_init(NULL);
}
mysql_test.c:
#include "../lib/fcts_mysql.h"
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
MYSQL *cnxion;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "192.168.1.2";
char *user = "root";
char *password = "";
char *database = "test";
// cnxion = mysql_init(NULL);
cnxion = cmd_mysql_init();
if (!mysql_real_connect(cnxion, server,user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n 404 \n", mysql_error(cnxion));return 1;
}
if (mysql_query(cnxion, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(cnxion));return 1;
}
res = mysql_use_result(cnxion);
// res = cmd_mysql_select();
printf("MySQL Tables in mysql database:\n");
// FILE *file = fopen("liste_table.txt","w");
while ((row = mysql_fetch_row(res)) != NULL){
// printf("%s \n", row[0]);fprintf(file,"%s \n",row[0]);
}
// fclose(file);
mysql_free_result(res);
mysql_close(cnxion);
return 0 ;
}
[root@xxxxx bin]# gcc mysql_test.c $(mysql_config --libs) $(mysql_config --cflags)
In file included from mysql_test.c:1:
../lib/fcts_mysql.h:6: erreur: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
【问题讨论】:
-
MYSQL未在fcts_mysql.h中定义。您可能应该先包含一个标题。 -
thnx @Biffen 为您提供帮助,但我尝试添加它并在收到如下错误后:/tmp/ccAvihja.o: In function
main': /root/projet/test/bin/mysql_test.c:21: undefined reference tocmd_mysql_init' collect2: ld a retourné 1 code d 'état d'execution -
这是一个链接错误。看起来您没有将二进制文件与(或一个)MySQL 库链接。
-
我不这么认为,因为我使用 gcc mysql_test.c $(mysql_config --libs) $(mysql_config --cflags) 来编译......如果你现在有其他方法来链接它请让我知道.. 非常感谢。
-
如果不了解您的设置,很难判断。也许您应该研究一个为您处理所有这些事情的构建系统,而不是手动运行命令。