嗨 我正在使用用 C 和 MySql 编写的 CGI。
这是我的示例页面。
// save user
void saveUser(char *name, char *pwd,char *role)
{
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",name,pwd,role);
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
/* close connection */
mysql_close(conn);
}
每当我将页面放入我的 CGI 文件夹时,我都会遇到此错误。
对 `mysql_init' 的 undefined reference
对“mysql_connect”的 undefined reference
对 `mysql_select_db' 的 undefined reference 和其他mysql 相关的错误。 当我在网上搜索解决方案时 它说我必须像那样将我的页面与 mysql 客户端库链接起来
gcc -o a $(mysql_config --cflags) saveUser.c $(mysql_config --libs)
第一个问题 这是否意味着我不能直接在我的 CGI 中使用 saveUser.c?
每当我将 saveuser.c 放入 CGI 文件夹时,它都会显示相同的错误。 我只能放我创建的脚本。 所以我决定使用 system("a %s %s %s",arg[0],arg[1],arg[2]) 从 CGI 调用 a 脚本 然后我可以保存用户。一切正常。
但是当我尝试从数据库中检索数据时遇到了另一个问题。 我不知道如何使用生成的脚本从数据库中获取数据。 我必须在我的网站上显示用户列表。 这是我的检索代码。
// get users
void getAll()
{
char query[500];
char result[1024];
memset(result,0,1024);
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
sprintf(query,"select * from userTbl");
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
res = mysql_use_result(conn);
/* output table name */
system("clear");
sprintf(result,"ID\t Name\t Password\t Role\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
//printf("%s \n", row[0]);
//strcpy(id,row[0]);
sprintf(query,"%s\t %s\t %s\t\t %s\n",row[0], row[1], row[2], row[3]);
strcat(result,query);
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
printf(result);
}
第二个问题 克服这个问题的最佳方法是什么。 我可以在我的 CGI 中使用 脚本 还是如何直接在我的 CGI 中使用 getAll.c。
谁能帮我解决我的问题? 我 1.5 个月前才学 CGI,2.5 个月前才学 C。 抱歉我的无知。
提前致谢。
更新:
这是我的顶级Makefile
ROOT=.
CURDIR=$(shell /bin/pwd)
JITCOMM_INSTALL=$(ROOT)/install
include $(ROOT)/Makefile.basic
#set root directory
SUB_DIRS = cgi_src
SUB_DIRS += check_update
SUB_DIRS += loadconfig
SUB_DIRS += keepalive
SUB_DIRS += script
SUB_DIRS += server
SUB_DIRS += startproxy
SUB_DIRS += net_stats
#SUB_DIRS += ../sniffer_gui
#SUB_DIRS += java
all:
ifneq ($(SUB_DIRS), )
@for i in $(SUB_DIRS) ; do if [ ! -d $(CURDIR)/$${i} ]; then continue; fi; \
cd $(CURDIR)/$${i}; make || exit; cd $(CURDIR); done
endif
clean:
@rm -f $(ROOT)/lib/*
@rm -rf $(JITCOMM_INSTALL)
ifneq ($(SUB_DIRS), )
@for i in $(SUB_DIRS) ; do if [ ! -d $(CURDIR)/$${i} ]; then continue; fi; \
cd $(CURDIR)/$${i}; make clean || exit; cd $(CURDIR); done
endif
install: all
@rm -rf $(JITCOMM_INSTALL)
@mkdir $(JITCOMM_INSTALL)
@mkdir $(JITCOMM_INSTALL)/etc
@mkdir $(JITCOMM_INSTALL)/etc/acpro
@mkdir $(JITCOMM_INSTALL)/etc/syslog-ng
@mkdir $(JITCOMM_INSTALL)/etc/apache2
@mkdir $(JITCOMM_INSTALL)/etc/apache2/sites-available
@mkdir $(JITCOMM_INSTALL)/var
@mkdir $(JITCOMM_INSTALL)/var/www
@mkdir $(JITCOMM_INSTALL)/var/www/cgi-bin
这是我的 cgi-src 文件夹中的 Makefile
ROOT=../
CURDIR=$(shell /bin/pwd)
include $(ROOT)/Makefile.basic
#set root directory
SUB_DIRS = util
SUB_DIRS += cgi_util
SUB_DIRS += cgi_module
#Must be last
SUB_DIRS += cgi_main
all:
ifneq ($(SUB_DIRS), )
@for i in $(SUB_DIRS) ; do if [ ! -d $(CURDIR)/$${i} ]; then continue; fi; \
cd $(CURDIR)/$${i}; make || exit; cd $(CURDIR); done
endif
clean:
ifneq ($(SUB_DIRS), )
@for i in $(SUB_DIRS) ; do if [ ! -d $(CURDIR)/$${i} ]; then continue; fi; \
cd $(CURDIR)/$${i}; make clean || exit; cd $(CURDIR); done
endif
install:
ifneq ($(SUB_DIRS), )
@for i in $(SUB_DIRS) ; do if [ ! -d $(CURDIR)/$${i} ]; then continue; fi; \
cd $(CURDIR)/$${i}; make install || exit; cd $(CURDIR); done
endif
这是我的 Makefile.basic
CC=/usr/bin/gcc
#CC=powerpc-linux-gcc
CP=/usr/bin/cp
CFLAGS=-g -Wall $(shell mysql_config --cflags) $(shell mysql_config --libs)
www=/var/www
htdocs=/htdocs
cgi_bin=/cgi-bin
config=/etc/apache2/sites-available/default
INSTALL_DIR=$(pwd)/.install
这是我的部分错误
/usr/bin/gcc -c -g -Wall -I/usr/include/mysql -DBIG_JOINS=1 -fPIC fno-strict- aliasing -Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient -I../include -o cgi_func.o cgi_func.c
/usr/bin/gcc -c -g -Wall -I/usr/include/mysql -DBIG_JOINS=1 -fPIC -fno-strict-aliasing -Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient -I../include -o cgic.o cgic.c
/usr/bin/gcc -g -Wall -I/usr/include/mysql -DBIG_JOINS=1 -fPIC -fno-strict-aliasing -Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient -static -o cgi_main cgi_func.o cgic.o -lcgi_util -lcgi_module -lutil -L../../lib -lm
../../lib/libcgi_module.a(users.o):(.data.rel.local+0x8): multiple definition of `password'
../../lib/libcgi_module.a(password.o):/home/jitcomm/intern_GUI/jit24_test_v2/cgi_src/cgi_module/password.c:292: first defined here
/usr/bin/ld: Warning: size of symbol `password' changed from 193 in ../../lib /libcgi_module.a(password.o) to 4 in ../../lib/libcgi_module.a(users.o)
/usr/bin/ld: Warning: type of symbol `password' changed from 2 to 1 in ../../lib/libcgi_module.a(users.o)
../../lib/libcgi_module.a(users.o): In function `save':
/home/jitcomm/intern_GUI/jit24_test_v2/cgi_src/cgi_module/users.c:17: multiple definition of `save'
../../lib/libcgi_module.a(mode.o):/home/jitcomm/intern_GUI/jit24_test_v2/cgi_src/cgi_module/mode.c:56: first defined here
../../lib/libcgi_module.a(users.o): In function `saveUser':
users.c:(.text+0x1cb): undefined reference to `mysql_init'
users.c:(.text+0x22d): undefined reference to `mysql_real_connect'
users.c:(.text+0x241): undefined reference to `mysql_error'
users.c:(.text+0x2bd): undefined reference to `mysql_query'
users.c:(.text+0x2d1): undefined reference to `mysql_error'
users.c:(.text+0x30d): undefined reference to `mysql_close'
最佳答案
您已经回答了您自己的问题,如果您得到对 MySQL 的 undefined reference ,则说明您没有正确链接它。您需要编译:
gcc -o a $(mysql_config --cflags) saveUser.c $(mysql_config --libs)
这与代码的外观/工作方式无关,只与编译器/链接器如何将源文件组合成可执行文件有关。如果您发布 Makefile 的一部分而不是您的代码,您可能会得到更多有关该问题的指示。
编辑:好的,您可以将 $(mysql_config --cflags) $(mysql_config --libs)
添加到 Makefile.basic 中的 CFLAGS 声明中,看看会发生什么吗?
EDIT2:导致错误的行似乎是:
/usr/bin/gcc -g -Wall -I/usr/include/mysql -DBIG_JOINS=1 -fPIC -fno-strict-aliasing -Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient -static -o cgi_main cgi_func.o cgic.o -lcgi_util -lcgi_module -lutil -L../../lib -lm
有点奇怪,因为(我假设)链接了正确的库 (-lmysqlclient
)...仔细检查您没有安装多个版本的 mysql 库,或者这些函数在不同的库中定义。最简单的方法是创建一个仅包含这些 mysql 函数的最小项目并尝试链接它。如果这不起作用,那就是你的问题。如果它确实有效,我不确定下一步该去哪里。
顺便说一句,你应该看看这些多重定义警告,看起来很讨厌……检查你是否在某个头文件中定义了全局变量。您正在处理的项目似乎将其所有代码分区在不同的库中并在最后链接这些代码,也许您需要清理所有这些代码(删除 .o 和 .a 文件)并再次运行 make?
关于mysql - 连接CGI(C编程)与Mysql时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6132985/