- 论坛徽章:
- 0
|
对于glibc,是不推荐静态链接的,但是某些情况下,需要这么做。问题是:静态链接所有库的时候,g++会抛出警告,程序执行的时候,会出错,google了一圈,也问了老大一圈,但是没有一个解释,可能是没人这么干吧。哪位达人帮忙,谢谢
附一个简单的例子,多线程里面调用 getaddrinfo(),静态链接所有库,程序会崩溃,在多个环境下测试:
- Ubuntu8.04.2
- RedHatEnterprise Linux AS release 4
- DebianGNU/Linux 3.1
- DebianGNU/Linux 5.0
g++ -static sample.cpp -lpthread
warning: Using 'getaddrinfo' in statically linked applicationsrequires at runtime the shared libraries from the glibc version usedfor linking
code:
#include <stdio.h>
#include <netdb.h>
#include <sys/time.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <errno.h>
#include <dlfcn.h>
typedef int (*f_getaddrinfo)(const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res);
typedef void (*f_freeaddrinfo)(struct addrinfo *res);
f_getaddrinfo myf_getaddr;
f_freeaddrinfo myf_freeaddr;
pthread_mutex_t g_mutex;
char * Get_Serv_IP(const char *hostname, char *ip_str)
{
printf("serv_hostname Get_Serv_IP gets is : %s\n", hostname);
struct hostent *hptr = NULL;
struct in_addr inAddrIp;
if (!inet_aton((char *)hostname, (struct in_addr *)&inAddrIp))
{
struct addrinfo hints, *result;
int rc;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_INET;
// pthread_mutex_lock( &g_mutex );
rc = getaddrinfo(hostname, NULL, &hints, &result);
// rc = myf_getaddr(hostname, NULL, &hints, &result);
// pthread_mutex_unlock( &g_mutex );
printf( "out from getaddrinfo from host:%s\n", hostname );
if( rc != 0 )
{
printf("getaddrinfo error for host:%s :%s",hostname,strerror(errno));
return NULL;
}
inAddrIp = ((sockaddr_in*)(result->ai_addr))->sin_addr;
printf( "before free addrinfo for host:%s\n", hostname );
freeaddrinfo(result);
// myf_freeaddr(result);
}
printf("serv_hostname after Get_Serv_IP is : %s\n",hostname);
inet_ntop(AF_INET,&inAddrIp,ip_str,INET_ADDRSTRLEN);
printf("dst:%s\n", ip_str);
return ip_str;
}
static std::string host_array[] = {
"www.163.com","www.baidu.com","www.google.com","www.sina.com","www.sohu.com","www.gameloft.com","","gateway.sandbox.push.apple.com","gateway.push.apple.com","feedback.sandbox.push.apple.com","feedback.push.apple.com",
"www.163.com","www.baidu.com","www.google.com","www.sina.com","www.sohu.com","www.gameloft.com","","gateway.sandbox.push.apple.com","gateway.push.apple.com","feedback.sandbox.push.apple.com","feedback.push.apple.com",
"www.163.com","www.baidu.com","www.google.com","www.sina.com","www.sohu.com","www.gameloft.com","","gateway.sandbox.push.apple.com","gateway.push.apple.com","feedback.sandbox.push.apple.com","feedback.push.apple.com",
"www.163.com","www.baidu.com","www.google.com","www.sina.com","www.sohu.com","www.gameloft.com","","gateway.sandbox.push.apple.com","gateway.push.apple.com","feedback.sandbox.push.apple.com","feedback.push.apple.com",
"www.163.com","www.baidu.com","www.google.com","www.sina.com","www.sohu.com","www.gameloft.com","","gateway.sandbox.push.apple.com","gateway.push.apple.com","feedback.sandbox.push.apple.com","feedback.push.apple.com",
"www.163.com","www.baidu.com","www.google.com","www.sina.com","www.sohu.com","www.gameloft.com","","gateway.sandbox.push.apple.com","gateway.push.apple.com","feedback.sandbox.push.apple.com","feedback.push.apple.com"
};
void * threadfun( void * arg )
{
long index = (long)(arg);
while( true )
{
char ip_str[100] = {0};
Get_Serv_IP( host_array[index].c_str(), ip_str );
// sleep(1);
}
return NULL;
}
int main()
{
pthread_mutex_init( &g_mutex, NULL );
int count = 0;
for( int i = 0; i <sizeof(host_array)/sizeof(host_array[0]); ++i )
{
pthread_t tid;
pthread_create( &tid, NULL, threadfun, (void*)i );
//if( ++count == 2 ) break;
}
sleep( 338899 );
return 0;
} |
|