无法编译简单的mysql程序

时间:2013-09-19 09:12:24

标签: mysql c compiler-construction

cblog.c:

#include "html.h"
#include "config.h"
#include <mysql/mysql.h>
//#include <my_global.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
        MYSQL *conn;
        MYSQL_RES *res;
        MYSQL_ROW row;

        conn = mysql_init(NULL);

        // Connect to database
        if (!msyql_real_connect(conn, server, username, password, database, 0, NULL, 0)) {
                fprintf(stderr, "%s\n", mysql_error(conn));
                exit(1);
        }

        // Query the DB
        if (mysql_query(conn, "show tables")) {
                fprintf(stderr, "%s\n", mysql_error(conn));
                exit(1);
        }
        res = mysql_use_result(conn);

        htmlf("MySQL Tables in mysql database:\n");
        while ((row = mysql_fetch_row(res)) != NULL)
                htmlf("%s \n", row[0]);

        mysql_free_result(res);
        mysql_close(conn);

        html("Hello World!");
        return 0;
}

生成文件:

all:
#       gcc -Wall cblog.c html.c -o cblog `mysql_config --cflags --libs`
#       gcc -Wall cblog.c html.c -L/usr/lib -lmysqlclient -o cblog
        gcc cblog.c html.c -Wall $(shell mysql_config --cflags) -o cblog $(shell mysql_config --libs)
test: all
        ./cblog

输出:

ttouch cblog$ make
gcc cblog.c html.c -g -Wall -I/usr/include/mysql -fPIC -pipe -fstack-protector --param=ssp-buffer-size=4 -fno-strict-aliasing -DBIG_JOINS=1 -fomit-frame-pointer -g -DNDEBUG -o cblog -L/usr/lib -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
cblog.c: In function ‘main’:
cblog.c:16:2: warning: implicit declaration of function ‘msyql_real_connect’ [-Wimplicit-function-declaration]
  if (!msyql_real_connect(conn, server, username, password, database, 0, NULL, 0)) {
  ^
/tmp/ccs1WQsi.o: In function `main':
/media/files/Lab/cblog/cblog.c:16: undefined reference to `msyql_real_connect'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

我知道这个问题已被回答了一千多次,我在互联网上搜索过,我已经测试了mysql_config在开头或结尾处。我知道它提供了正确的路径(已选中)

ls /usr/lib/ | grep mysql

libmysqlclient.a
libmysqlclient_r.a
libmysqlclient_r.so
libmysqlclient_r.so.18
libmysqlclient_r.so.18.0.0
libmysqlclient.so
libmysqlclient.so.18
libmysqlclient.so.18.0.0
libmysqld.a
libmysqld.so
libmysqld.so.18
libmysqlservices.a
mysql
tdbcmysql1.0.0

ls /usr/include/mysql | grep mysql

mysql_com.h
mysqld_ername.h
mysqld_error.h
mysql_embed.h
mysql.h
mysql_time.h
mysql_version.h

对于这个愚蠢的问题感到抱歉,但我已经敲了大约4-5个小时

1 个答案:

答案 0 :(得分:3)

您在函数名称中输入了拼写错误,msyql_real_connect应为mysql_real_connect