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 个小时