Hyperlink 静态链接libmysqlclient ubuntu 12.04 32位

Hyperlink 静态链接libmysqlclient ubuntu 12.04 32位,hyperlink,static,static-linking,libmysql,Hyperlink,Static,Static Linking,Libmysql,我正在使用XAMPP,并尝试在虚拟机中干净地安装Ubuntu 12.04,结果是一样的 以下程序可轻松编译和链接: gcc c_mysql.c-l mysqlclient-o c_mysql 也就是说,生成默认的动态相似程序 但是获得一个静态链接的程序并不是那么容易: 在广泛的谷歌搜索之后,我先后尝试了以下方法。任何帮助都将是免费的 非常感谢: 1) gcc c_mysql1.c libmysqlclient.a -o c_mysql [Library libmysqlclient.a not

我正在使用XAMPP,并尝试在虚拟机中干净地安装Ubuntu 12.04,结果是一样的

以下程序可轻松编译和链接:

gcc c_mysql.c-l mysqlclient-o c_mysql

也就是说,生成默认的动态相似程序

但是获得一个静态链接的程序并不是那么容易:

在广泛的谷歌搜索之后,我先后尝试了以下方法。任何帮助都将是免费的 非常感谢:

1) gcc c_mysql1.c libmysqlclient.a -o c_mysql
[Library libmysqlclient.a not found]

2) gcc c_mysql1.c /usr/lib/i386-linux-gnu/libmysqlclient.a -o c_mysql
[Many undefined references]

3) gcc c_mysql1.c /usr/lib/i386-linux-gnu/libmysqlclient.a /usr/lib/i386-linux-gnu/libdl.a 
[Many but less undefined references]

4) gcc c_mysql1.c /usr/lib/i386-linux-gnu/libmysqlclient.a /usr/lib/i386-linux-gnu/libdl.a -lpthread -lz -o c_mysql
[The minimum undefined references I could get]

/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x524): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x42): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x50): undefined reference to `log'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x62): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libdl.a(dlopen.o): In function `dlopen':
(.text+0x1b): undefined reference to `__dlopen'
/usr/lib/i386-linux-gnu/libdl.a(dlclose.o): In function `dlclose':
(.text+0x1): undefined reference to `__dlclose'
/usr/lib/i386-linux-gnu/libdl.a(dlsym.o): In function `dlsym':
(.text+0x1b): undefined reference to `__dlsym'
/usr/lib/i386-linux-gnu/libdl.a(dlerror.o): In function `dlerror':
(.text+0x1): undefined reference to `__dlerror'
collect2: ld returned 1 exit status
-静态会产生更多未定义的引用——静态libgcc甚至是最糟糕的

/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x524): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(mf_pack.c.o): In function `unpack_dirname':
(.text+0x653): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(libmysql.c.o): In function `read_user_name':
(.text+0x2a91): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(mf_pack.c.o): In function `unpack_dirname':
(.text+0x667): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(client.c.o): In function `mysql_real_connect':
(.text+0x47b6): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(libmysql.c.o): In function `mysql_server_init':
(.text+0x27fa): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x42): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x50): undefined reference to `log'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x62): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_compress_alloc':
(.text+0x68): undefined reference to `compress'
/usr/lib/i386-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_uncompress':
(.text+0x1cf): undefined reference to `uncompress'
collect2: ld returned 1 exit status



#include <mysql/mysql.h> 
#include <stdio.h> 
#include <string.h> 
#include <pthread.h>

int main() 
{ 
     MYSQL mysql; 
     MYSQL_RES *res; 
     MYSQL_ROW row; 
     char *query = "select * from dbtablename;"; 
     int t,r; 

     mysql_init(&mysql); 
   //if(!mysql_real_connect(&mysql,"localhost",      "dbusername",      "dbpassword", "dbname",            port, NULL, 0)) The connection data is fake. If you, for a big miracle make it compile statically, the program will not connect.
     if(!mysql_real_connect(&mysql,"192.231.182.73","PinData","YULYU7M",     "DB084",  0,  NULL, 0)){ 
         printf("Error connecting to database:%s\n",mysql_error(&mysql)); 
     } 
     else{
         printf("Connected to the remote database........");
     } 
     t=mysql_query(&mysql,query); 
     if(t) 
     { 
         printf("Error making query:%s\n",mysql_error(&mysql)); 
     } 
     else
     { 
         printf("Query made ....\n"); 
         res = mysql_use_result(&mysql); 
         if(res) 
         { 
             for(r=0;r<=mysql_field_count(&mysql);r++) 
             { 
                 row = mysql_fetch_row(res); 
                 if(row<0) break; 
                 for(t=0;t<mysql_num_fields(res);t++) 
                     printf("%s ",row[t]); 
                 printf("\n"); 
             } 
         } 
         mysql_free_result(res); 
     } 
     mysql_close(&mysql); 
     return 0; 
 }

经过多次尝试和谷歌搜索,答案是:要静态链接libmysqlclient,您必须使用以下选项:

-lpthread-lm-lz-ldl

这使得链接器可以在某些库中找到

即:

gcc-static libgcc c_mysql1.c/usr/lib/i386 linux gnu/libmysqlclient.a-lpthread-lm-lz-ldl-o c_mysql

然而,一般的问题仍然是开放的:给定一个静态库StaticLib,如何提前知道要使用哪些选项和库

我不敢相信答案会是反复试验和谷歌搜索