【问题标题】:Why do I get this error when I compile my MySQL connection test?为什么我在编译 MySQL 连接测试时会收到此错误?
【发布时间】: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 to cmd_mysql_init' collect2: ld a retourné 1 code d 'état d'execution
  • 这是一个链接错误。看起来您没有将二进制文件与(或一个)MySQL 库链接。
  • 我不这么认为,因为我使用 gcc mysql_test.c $(mysql_config --libs) $(mysql_config --cflags) 来编译......如果你现在有其他方法来链接它请让我知道.. 非常感谢。
  • 如果不了解您的设置,很难判断。也许您应该研究一个为您处理所有这些事情的构建系统,而不是手动运行命令。

标签: mysql c linux


【解决方案1】:

MYSQL"../lib/fcts_mysql.h" 中得到它时,编译器并不知道它。

#include &lt;mysql.h&gt; 移动到#include "../lib/fcts_mysql.h" 前面将解决此问题。

【讨论】:

  • 最好让fcts_mysql.h 自己包含所有需要的标头。
猜你喜欢
  • 2012-03-13
  • 1970-01-01
  • 2023-03-05
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 2016-01-01
相关资源
最近更新 更多