Compile a static binary which code there a function gethostbyname
What you are asking for is going to be very difficult.
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.
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.
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!
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.
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.
Floating point exception using function gethostbyname in static linked application
static linking considered harmful
http://www.akkadia.org/drepper/no_static_linking.html
"all kinds of features in the libc (locale (through iconv), NSS, IDN, ...) require dynamic linking to load the appropriate external code."
Create statically-linked binary that uses getaddrinfo?
glibc uses libnss to support a number of different providers for address resolution services. Unfortunately, you cannot statically link libnss, as exactly what providers it loads depends on the local system's configuration.
Related Topics
Automatic Copy Files to Output During Application Building
How to Pass an Std::String to Glshadersource
C++ "Named Parameter Idiom" VS. Boost::Parameter Library
C++ Array of Pointers: Delete or Delete []
How to Simulate Interfaces in C++
Alternative to C++ Static Virtual Methods
Difference Between Long Double and Double in C and C++
How to Pan Images in Qgraphicsview
Implementing Future::Then() Equivalent for Asynchronous Execution in C++11
Is Using a Vector of Boolean Values Slower Than a Dynamic Bitset
Brute-Force, Single-Threaded Prime Factorization
Efficiency of Postincrement V.S. Preincrement in C++
C++ Specialization of Template Function Inside Template Class
Undefined Reference to Winmain@16 C++, Sdl-2
Constructor Conditionally Marked Explicit
Quick Sort at Compilation Time Using C++11 Variadic Templates