博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
静态编译mysql库到程序中遇到的问题
阅读量:7080 次
发布时间:2019-06-28

本文共 3450 字,大约阅读时间需要 11 分钟。

  最近有个项目需要生成静态编译的可执行文件,以便在其它linux的机器中运行,开始以为非常简单,直接在编译中加个-static选项不就是了,结果却和我想的太不一样了,下面说下我遇到的问题以及解决的方法。

  开始按照设想应该只要在编译中加个-static选项就可以了,不过却报下面的错误:

cc -g -static -o test_server  main_server.o main_db.o err_me.o  -L/usr/lib/mysql/ -lmysqlclient  -lpthread  -ldl -lcrypt/usr/bin/ld: cannot find -lmysqlclient/usr/lib/gcc/i686-redhat-linux/4.6.2/http://www.cnblogs.com/../libpthread.a(libpthread.o): In function `sem_open':(.text+0x6917): warning: the use of `mktemp' is dangerous, better use `mkstemp'collect2: ld returned 1 exit statusmake: *** [test_server] Error 1

  说是没有找到-lmysqlclient,应该是没有找到libmysqlclient.a库,然后我到目录/usr/lib/mysql/下去找,只有libmysqlclient.so的动态链接库,因为加了-static后要使用静态库才能编译成功,然后就在网上搜看看有没有libmysqlclient.a的静态库下载。搜了好几个小时一无所获,偶然看到一个也是编译与mysql库有关的程序的选项,发现里面库的路径居然是/usr/local/mysql/lib/,然后我也到这个目录去看了下,libmysqlclient.a文件真的在这里面,汗!!!这才想起自己以前是用源代码安装的mysql和mysql-dev的。

  有了libmysqlclient.a库文件,我立马加入编译选项中重新编译,本以为万事大吉了,结果还是报下面的错:

cc -g -static -o test_server  main_server.o main_db.o err_me.o  -L/usr/local/mysql/lib/ -lmysqlclient  -lpthread  -ldl -lcrypt/usr/local/mysql/lib//libmysqlclient.a(mf_pack.c.o): In function `unpack_dirname':mf_pack.c:(.text+0x6dd): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking/usr/local/mysql/lib//libmysqlclient.a(libmysql.c.o): In function `read_user_name':libmysql.c:(.text+0x2f21): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking/usr/local/mysql/lib//libmysqlclient.a(mf_pack.c.o): In function `unpack_dirname':mf_pack.c:(.text+0x6ed): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking/usr/local/mysql/lib//libmysqlclient.a(client.c.o): In function `mysql_real_connect':client.c:(.text+0x34b6): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking/usr/lib/gcc/i686-redhat-linux/4.6.2/http://www.cnblogs.com/../libpthread.a(libpthread.o): In function `sem_open':(.text+0x6917): warning: the use of `mktemp' is dangerous, better use `mkstemp'/usr/local/mysql/lib//libmysqlclient.a(libmysql.c.o): In function `mysql_server_init':libmysql.c:(.text+0x2a4a): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking/usr/local/mysql/lib//libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':dh.cpp:(.text+0x1a8): undefined reference to `pow'dh.cpp:(.text+0x1b8): undefined reference to `log'dh.cpp:(.text+0x1ca): undefined reference to `pow'collect2: ld returned 1 exit statusmake: *** [test_server] Error 1

  先不管前面的警告,和面说明定义pow,然后再网上搜了下,必须在编译选项中加-lm,然后再编译下,终于是生成了可执行文件了。但是前面的警告是怎么回事,”warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking“,使用getpwnam的应用程序需要运行时共享库glibc来链接。在网上找了很久也没有找到怎么把警告去掉的方法,不过有个折中的方面,貌似也正是解决这个问题的方法,就是libmysqlclient.a库用静态连接,一些常用的库用动态连接,因为程序是运行在linux中的,常用库系统默认都会有的。一部分静态连接,一部分动态连接的方法是:-Wl,-dn后面是静态链接-Wl,-dy后面是动态连接,具体如下:

cc -g -o test_server  main_server.o main_db.o err_me.o -Wl,-dn -L/usr/local/mysql/lib/ -lmysqlclient  -Wl,-dy -lpthread -lm -ldl -lcrypt

  总结:gcc很强大,自己学的只有皮毛,以后还要多用才行。

你可能感兴趣的文章
PHP Ajax 跨域问题最佳解决方案
查看>>
Linux之开源软件移植
查看>>
C#.NET SQLite自适应32位/64位系统
查看>>
stl本子
查看>>
浅谈大型网站动态应用系统架构(转)
查看>>
iOS 日期时间控件
查看>>
ABAP 四舍五入函数
查看>>
JDBC使用步骤分哪几步?
查看>>
线段树心得
查看>>
Linux 进程间通信 信号灯集
查看>>
Python Day 8: html 基本知识
查看>>
2012年4月19日
查看>>
UVA 11090 Going in Cycle!! 二分答案 + bellman-ford
查看>>
final,static,super,this
查看>>
LeetCode解题思路:442. Find All Duplicates in an Array
查看>>
解决BCG库示例程序中的一个诡异编译错误
查看>>
Linux常用命令
查看>>
gradle-遇到的问题
查看>>
C#生成唯一的字符串或者数字
查看>>
深入理解this对象
查看>>