Dan Kegel <[EMAIL PROTECTED]> writes:

> Standard practice is probably to always write

> #if HAVE_SYS_TYPES_H
> #include <sys/types.h>
> #endif

There's no need to check for the existance of <sys/types.h> unless you're
attempting portability far beyond that any normal software package will
need.  It exists on pretty much any modern platform and nearly all
obsolete ones.

> #if HAVE_SYS_SOCKET_H
> #include <sys/socket.h>
> #endif

Likewise here; if it has the socket functions, it will have
<sys/socket.h>.

>> One of the reasons for this is that it uses the socket()
>> function. Under FreeBSD this is in libc, and requires the following
>> header files to be included:

>>      #include <sys/types.h>
>>      #include <sys/socket.h>

>> The same is also true of GNU/Linux.

>> However, under Solaris (SunOS 5.6) you must compile againt -lxnet and
>> include only

>>      #include <sys/socket.h>

It's harmless to include <sys/types.h> as well.

You probably don't want -lxnet.  You probably want -lsocket.  Don't pay
too much attention to the Solaris man pages unless you're sure you have
the right section.

>> There may be other methods of getting socket() on other OSes, for all I
>> know.

>> What is the correct way to diagnose how to get socket(), or any other
>> function, on a given OS, and to build the software appropriately, using
>> autoconf?

Always include <sys/types.h> and <sys/socket.h>.  Then use something like
this, which should work pretty well:

dnl The rat's nest of networking libraries.  The common cases are not to
dnl need any extra libraries, or to need -lsocket -lnsl.  We need to avoid
dnl linking with libnsl unless we need it, though, since on some OSes where
dnl it isn't necessary it will totally break networking.  Unisys also
dnl includes gethostbyname in libsocket but needs libnsl for socket().
AC_SEARCH_LIBS(gethostbyname, nsl)
AC_SEARCH_LIBS(socket, socket, ,
    [AC_CHECK_LIB(nsl, socket, LIBS="$LIBS -lsocket -lnsl", , -lsocket)])

-- 
Russ Allbery ([EMAIL PROTECTED])             <http://www.eyrie.org/~eagle/>


Reply via email to