链接C和Mysql中的错误

时间:2011-05-27 09:49:38

标签: mysql c makefile cgi

您好我正在开发用C语言编写并尝试使用Mysql的CGI。 当我尝试使用它时,我得到了以mysql_开头的符号的未定义引用错误,如下所示:

/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:18: 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+0x192): undefined reference to `mysql_init'
users.c:(.text+0x1e4): undefined reference to `mysql_real_connect'
users.c:(.text+0x1f5): undefined reference to `mysql_error'
users.c:(.text+0x267): undefined reference to `mysql_query'
users.c:(.text+0x278): undefined reference to `mysql_error'
users.c:(.text+0x2ab): undefined reference to `mysql_close'

所以我写了一些示例代码,当我使用我的程序时,我可以运行良好

gcc -o saveUser $(mysql_config --cflags) saveUser.c $(mysql_config --libs)

效果很好。

下一步,我尝试将我的编码放入CGI中。 我仍然得到那些未定义的引用。 这是我的Makefile.basic

CC=/usr/bin/gcc
#CC=powerpc-linux-gcc
CP=/usr/bin/cp
CFLAGS = -g -Wall $(mysql_config --cflags) $(mysql_config --libs)

www=/var/www
htdocs=/htdocs
cgi_bin=/cgi-bin
config=/etc/apache2/sites-available/default

我该怎么办?哪里出错了?请帮帮我。我一直在互联网上搜索一周,但仍无法得到解决方案。我猜它在 makefile.basic 中,并且链接到mysql是错误的。

由于

更新:

这是我的顶级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

最新更新:

cgi_module中的Makefile

#set common variant
.SUFFIXES: .c .h .o

ROOT=../../
include $(ROOT)/Makefile.basic

#LINK_NAME = changepass.cgi login.cgi network.cgi reboot.cgi shutdown.cgi
LINK_NAME = login.cgi

INCS = -I../include
INCS += -I../../cgi_src/util/include
OBJES=../../lib/


TARGET = libcgi_module.a
#don't change below
LIB_OBJS = $(patsubst %.c, %.o, $(wildcard *.c))
CLEAN_OBJS = $(LIB_OBJS) $(TARGET)

INST_OBJ = $(TARGET)
%.o: %.c
        $(CC) -c $(CFLAGS) $(INCS) -o $@ $<

all: $(TARGET)

$(TARGET): $(LIB_OBJS)
        $(AR) -vsur $@ $^ 
        @cp $(TARGET) $(OBJES)

clean:
        -@rm -rf $(CLEAN_OBJS)

install:

1 个答案:

答案 0 :(得分:1)

试试这个

CC=/usr/bin/gcc
#CC=powerpc-linux-gcc
CP=/usr/bin/cp
CFLAGS = -g -Wall $(shell mysql_config --cflags) $(shell mysql_config --libs)


foo:
    @echo "CFLAGS are $(CFLAGS)"

并启动make foo