1

I am using XAMPP and tried with a clean install of Ubuntu 12.04 in a VM, the results are the same.

The program bellow compile and link easily with:

gcc c_mysql.c -l mysqlclient -o c_mysql

That is, produces the default dynamically liked program.

But getting a statically linked program is not so easy:

I tried successively the following ways after extensive googling. Any help will be really appreciated:

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 makes many more undefined references (-static-libgcc is even worst).

/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; 
 }
ScottJShea
  • 7,041
  • 11
  • 44
  • 67

1 Answers1

1

After many trial and error and google search, the answer is: To statically link libmysqlclient you have to use the following options:

-lpthread -lm -lz -ldl

This makes the linker find on some libraries.

That is:

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

However the general question is still open: Given a static library StaticLib, how to know in advance which options and libraries to use.

I can not believe the answer would be " trial and error and google search"