What is the canonical way to create autoconf link in Solaris networking libraries?

On Solaris, when you compile a program that uses sockets, you need to link it to -lnsl -lsocket

. Many of these programs were originally written for Linux (where no additional libraries are needed), and therefore do not test these libraries in your configure scripts, although this is a fairly simple addition. Something like this (untested):

AC_SEARCH_LIBS(gethostbyname, nsl, , AC_MSG_ERROR([gethostbyname not found]))
AC_SEARCH_LIBS(connect, socket, , AC_MSG_ERROR([connect not found]))

      

Is there a canonical way to do this check? Maybe even included in the autoconf distribution? You might assume there is a fairly common need for this, but Google wouldn't tell me.

+2


source to share


2 answers


I think the closest thing to a canonical way to check is this macro AX_LIB_SOCKET_NSL

from the Autoconf Archive :



# ===========================================================================
#        http://www.nongnu.org/autoconf-archive/ax_lib_socket_nsl.html
# ===========================================================================
#
# SYNOPSIS
#
#   AX_LIB_SOCKET_NSL
#
# DESCRIPTION
#
#   This macro figures out what libraries are required on this platform to
#   link sockets programs.
#
#   The common cases are not to need any extra libraries, or to need
#   -lsocket and -lnsl. We need to avoid linking with libnsl unless we need
#   it, though, since on some OSes where it isn't necessary it will totally
#   break networking. Unisys also includes gethostbyname() in libsocket but
#   needs libnsl for socket().
#
# LICENSE
#
#   Copyright (c) 2008 Russ Allbery <rra@stanford.edu>
#   Copyright (c) 2008 Stepan Kasal <kasal@ucw.cz>
#   Copyright (c) 2008 Warren Young <warren@etr-usa.com>
#
#   Copying and distribution of this file, with or without modification, are
#   permitted in any medium without royalty provided the copyright notice
#   and this notice are preserved.

AU_ALIAS([LIB_SOCKET_NSL], [AX_LIB_SOCKET_NSL])
AC_DEFUN([AX_LIB_SOCKET_NSL],
[
        AC_SEARCH_LIBS([gethostbyname], [nsl])
        AC_SEARCH_LIBS([socket], [socket], [], [
                AC_CHECK_LIB([socket], [socket], [LIBS="-lsocket -lnsl $LIBS"],
                [], [-lnsl])])
])

      

+5


source


I can't remember any ready-made code from the top of my head, but you usually want to check if you can link the program calling the function gethostbyname()

without any additional libs. Only if it fails do you want to try the library nsl

.



Similar actions apply, for example, for a library m

.

0


source







All Articles