1

我正在使用 XAMPP 并尝试在 VM 中全新安装 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

-static产生更多未定义-static-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; 
 }
4

1 回答 1

1

经过多次反复试验和谷歌搜索,答案是:要静态链接 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,如何提前知道要使用哪些选项和库。

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

于 2014-08-17T12:04:33.793 回答