CentOS 64位MySQL编译

来源:互联网 发布:python 模拟登录 编辑:程序博客网 时间:2024/05/24 15:42
前提,CentOS 装有gcc和g++ 编译环境。

 mysql编译错误:undefined reference to `dlclose' Linux
 
一、错误现象


      在linux编译mysql代码,出现如下错误:
//================================================================//
 /usr/lib64/mysql/libmysqlclient.a(client_plugin.c.o): In function `add_plugin':
/export/home/pb2/build/sb_0-9651683-1372794821.34/rpm/BUILD/mysql-5.6.12/mysql-5.6.12/sql-common/client_plugin.c:178: undefined reference to `dlclose'
/usr/lib64/mysql/libmysqlclient.a(client_plugin.c.o): In function `mysql_client_plugin_deinit':
/export/home/pb2/build/sb_0-9651683-1372794821.34/rpm/BUILD/mysql-5.6.12/mysql-5.6.12/sql-common/client_plugin.c:280: undefined reference to `dlclose'
/usr/lib64/mysql/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
/export/home/pb2/build/sb_0-9651683-1372794821.34/rpm/BUILD/mysql-5.6.12/mysql-5.6.12/sql-common/client_plugin.c:367: undefined reference to `dlopen'
/export/home/pb2/build/sb_0-9651683-1372794821.34/rpm/BUILD/mysql-5.6.12/mysql-5.6.12/sql-common/client_plugin.c:396: undefined reference to `dlsym'
/export/home/pb2/build/sb_0-9651683-1372794821.34/rpm/BUILD/mysql-5.6.12/mysql-5.6.12/sql-common/client_plugin.c:387: undefined reference to `dlerror'
/export/home/pb2/build/sb_0-9651683-1372794821.34/rpm/BUILD/mysql-5.6.12/mysql-5.6.12/sql-common/client_plugin.c:399: undefined reference to `dlclose'
collect2: ld returned 1 exit status
//================================================================//


二、原因


      要对使用dlopen的文件进行编译时,需要加上 -ldl。


三、解决办法


      在Makefile中,加上-ldl。


      LINK_OPTIONS := \
            -lstdc++ \
            -lrt \
            -lpthread \
            -ldl 


1. 制作可执行文件Makefile 实例 CentOS 64位
//******************************************************************//
FLDINC = ../
LIBSPATH = ../src


#export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/lib64"


# 链接选项,必不可少.
# -ldl 
# -lrt 
# -lm 
# -lstdc++ 
# -lpthread 
# -L/usr/lib64/mysql -lmysqlclient
# 链接都不可少
#
LIB= -L $(LIBSPATH) -ldbf -ldl -lrt -lm -lstdc++ -lpthread -L/usr/lib64/mysql -lmysqlclient


ALLINC=  -I$(FLDINC)
CFLAGS=-O $(ALLINC)
LIBOBJ=  test.o
.c.o:
$(CC) -c $(CFLAGS) $*.c


EXE = test


all:  $(EXE) clean
test: test.c
$(CC) -o test $(ALLINC) $(CFLAGS) test.c $(LIB)         
clean:
rm -f *.o
//******************************************************************//


2. 制作可静态库Makefile 实例CentOS 64位


//******************************************************************//


LIB_FILES = endian.c dbf.c dbf2mysql.c mysql2dbf.c
#Mac OSX
#export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/"
#CentOS 64 bit
#export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/lib64/mysql"
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/lib64"
#ALLINC= -I/usr/local/include/mysql -L/usr/local/mysql/lib -lmysqlclient
#ALLINC= -I/usr/include/mysql -L/usr/lib64 -lmysqlclient
#ALLINC= -I/. -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient
#注意: 此时不能链接(解析如下)
ALLINC= -I/. -I/usr/include/mysql
CFLAGS= -O $(ALLINC)


#Library not loaded: libmysqlclient.18.dylib in OS X
#else sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
#change link
#export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/"


# All source files have associated object files
LIBOFILES = $(LIB_FILES:%.c=%.o)       


# all is the default rule
all: libdbf.a clean


# remove the old tapestry library and remake the new one
libdbf.a: $(LIBOFILES)
rm -f $@
ar cq $@ $(LIBOFILES)
clean:
rm -f $(LIBOFILES)
///////////////////
A. 解释1:


The problem comes from this part of the makefile:


$(PROJECT).o : $(SRC)/$(PROJECT).c
        $(MPICC) $(CFLAGS) $(LIBS) -c $(SRC)/$(PROJECT).c
At this step you are only invoking the compiler. The -c switch tells the compiler only to compile to an object file, and the linker is not involved at all. Since there is nothing to link, the $(LIBS) part is unnecessary.


The actual linking is done at the following stage:


$(PROJECT).exe : $(OBJECTS)
        $(MPICC) $(CFLAGS) $(LIBS) $(OBJECTS) -o $(PROJECT).exe
This is where the individual object files are merged together with the libraries to produce an executable. The compiler itself is not invoked at this point because the source files have already been transformed into object files.


B. 解释2
gcc -c与-l -- linker input file unused because linking not done
在项目中增加了新的库,重构了一下项目,依赖关系更加复杂了,于是在一个单独的项目文件夹中也放了一个makefile进去,make一下出现了 linker input file unused because linking not done 的提示。查了半天也没有头绪,最后突然领悟


使用-c是编译成.o的中间文件,这一步只检查语法错误,不检查依赖关系,所以不能加-l,因为既然不检查依赖关系,你给他指明链接文件,岂不多余
但是-l还是要加的,只要在使用.o文件最终链接时加上就可以了,无论依赖关系多么复杂,只要记住一个c一个o,然后把一个文件夹里的o弄到一个链接库a里,最后用所有的o,-l所有的a,链接成为可执行文件


一句话,编译是编译,链接是链接,编译的时候不能链接,链接的时候不要再编译,所以-c与-l两个选项不能同时使用
//******************************************************************//





原创粉丝点击