免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2162 | 回复: 0
打印 上一主题 下一主题

【难题求解】静态链接glibc,程序崩溃 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-25 16:25 |只看该作者 |倒序浏览
对于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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP