编译静态二进制文件,code有一个函数的gethostbyname [英] Compile a static binary which code there a function gethostbyname

查看:211
本文介绍了编译静态二进制文件,code有一个函数的gethostbyname的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决编译静态二进制其中code包括函数的gethostbyname如果编译时没有这样的警告:

How to resolve compile a static binary which code include a function gethostbyname and if compiled without warning like this:

警告:在静态链接的应用程序中使用'的gethostbyname
  需要在运行时从glibc的版本中使用的共享库
  用于连接

warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

我编译在Ubuntu 12.04使用命令:

I compile on ubuntu 12.04 with command:

$ gcc -static lookup.c -o lookup

这是code为lookup.c:

This is code for lookup.c:

  /* lookup.c */

  #include <stdio.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include <string.h>
  #include <errno.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <netdb.h>

  extern int h_errno;

  int main(int argc,char **argv) {
     int x, x2;
     struct hostent *hp;

     for ( x=1; x<argc; ++x ) {
        hp = gethostbyname(argv[x]);
        if ( !hp ) {
           fprintf(stderr,
                   "%s: host '%s'\n",
                   hstrerror(h_errno),
                   argv[x]);
           continue;
        }

        printf("Host %s : \n" ,argv[x]);
        printf(" Officially:\t%s\n", hp->h_name);
        fputs(" Aliases:\t",stdout);
        for ( x2=0; hp->h_aliases[x2]; ++x2 ) {
           if ( x2 ) {
              fputs(", ",stdout);
             }
        fputs(hp->h_aliases[x2],stdout);
        }     
        fputc('\n',stdout);
        printf(" Type:\t\t%s\n",
               hp->h_addrtype == AF_INET
               ? "AF_INET" : "AF_INET6");
        if ( hp->h_addrtype == AF_INET ) {
           for ( x2=0; hp->h_addr_list[x2]; ++x2 ) {
              printf(" Address:\t%s\n",
                     inet_ntoa( *(struct in_addr *)
                      hp->h_addr_list[x2]));
           }
        }
     putchar('\n');
     }
     return 0;
  }

我想,如果我通过 $文件查找会输出这样的检查:

查询:ELF 32位LSB的可执行文件,英特尔80386,版本1(GNU / Linux)上,
  静态链接,为GNU / Linux 2.6.24,
  BuildID [SHA1] = 0x6fcb2684ad8e5e842036936abb50911cdde47c73,不可剥离

lookup: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0x6fcb2684ad8e5e842036936abb50911cdde47c73, not stripped

不喜欢这样的:

查询:ELF 32位LSB的可执行文件,英特尔80386,版本1(SYSV)
  动态链接(使用共享库),为GNU / Linux 2.6.24,
  BuildID [SHA1] = 0xf9f18671751927bea80de676d207664abfdcf5dc,不可剥离

lookup: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xf9f18671751927bea80de676d207664abfdcf5dc, not stripped

如果您有建议,因为libc中我知道每一个Linux不同,我希望你不需要发表评论我必须不带静电使用评论。
为什么我坚持静态?
因为我需要做的强制使用静态的,二进制文件必须是静态的,不是动态的。

If you commented with suggested I must use without static because different libc every linux I knew it, I hope you do not need to comment. Why do I persist in for static? Because there I need to do to mandatory use static, binary files must be static and not dynamic.

我有2周以上找这个,但至今都没有成功。

I have more than 2 weeks looking for this but so far have not succeeded.

感谢您的帮助我解决我的沉重的问题。

Thanks for help me to resolve my heavy problem.

推荐答案

你所要求的将是非常困难的。

What you are asking for is going to be very difficult.

请参阅this有关的getaddrinfo 的StackOverflow问题。基本上,下面的getaddrinfo / 的gethostbyname 是的glibc的NSS层。这使得系统管理员说使用DNS主机名解析为IP地址或使用LDAP或不使用以外的任何其他 / etc / hosts中 。这种控制是在运行时;系统管理员可以在任何时候更改主机名解析为IP地址的方式。

See this StackOverflow question about getaddrinfo. Basically, underneath getaddrinfo/gethostbyname is glibc's NSS layer. This allows a sysadmin to say "use DNS for resolving hostnames to IP addresses", or "use LDAP", or "don't use anything other than /etc/hosts". This control is at runtime; the sysadmin can at any point change the way hostnames are resolved to IPs.

由于这种灵活性,所有的glibc使用辅助库名称解析调用(插件,基本上)做决议繁重的工作。有寻址,一个文件,一个DNS,一个是YP,等等等等LDAP的一个共享库。

Because of this flexibility, all of the name-resolution calls in glibc use helper libraries (plugins, basically) to do the grunt work of resolution. There's one shared library for LDAP addressing, one for files, one for DNS, one for YP, and so on and so on.

如果你希望你的程序是100%,静态链接,你将不得不去其他地方(而不是的gethostbyname )为主机名转换为IP地址。你可以用一个解析器库如 uDNS (这不是一个确切 - 有可用的类似工具)这样做,但你应该记住,您的二进制文件不会做这些配置为不使用DNS系统的正确的事情

If you want your program to be 100% statically linked, you're going to have to go elsewhere (NOT gethostbyname) to convert a hostname to an IP address. You could do this with a resolver library like uDNS (not this exact one - there are similar tools available), but you should keep in mind that your binary is not going to do the right thing on systems which are configured not to use DNS!

相反,我会建议刚离开程序(技术上)动态链接。如果你真的想确保它在任何平台上运行,你甚至可以运的glibc 与二进制 - 尽管这样做将需要LGPL的一致性。留在原位这个动态链接只会意味着你不会对系统的错误的glibc 版本中运行 - 而不是一个巨大的兼容性问题

Instead, I would recommend just leaving the program (technically) dynamically linked. If you really want to make sure it will run on any platform, you could even ship glibc with the binary - although doing this would require LGPL conformance. Leaving this one dynamic link in place will only mean you won't work on systems with the wrong glibc version - not a huge compatibility issue.

许可证合规性来讲,值得注意的是,如果你静态链接的glibc ,你最有可能出货源$ C ​​$ C为您的整个应用程序的符合的glibc 的LGPL许可。我不是律师,这是不是合格的法律意见,但看完LGPL非常清楚应用程序静态链接的glibc 必须是开放源代码。见<一href=\"http://stackoverflow.com/questions/11693320/linking-glibc-statically-and-pro$p$pitary-software-licensing\">this在话题 StackOverflow的问题。

Speaking of license compliance, it's worth noting that if you statically link glibc, you most likely have to ship the source code for your entire application to comply with glibc's LGPL license. I am not a lawyer, and this is not qualified legal advice, but reading the LGPL makes it very clear that applications statically linking glibc must be open-source. See this StackOverflow question on the topic.

这篇关于编译静态二进制文件,code有一个函数的gethostbyname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆