2

在 Solaris 上,当您编译使用套接字的程序时,您需要将它与-lnsl -lsocket. 许多此类程序最初是为 Linux 编写的(不需要额外的库),因此不会在其配置脚本中检查这些库,即使这是一个相当简单的添加。像这样的东西(未经测试):

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

有没有一种规范的方法来做这个检查?甚至可能包含在 autoconf 发行版中?你会想象有一个相当广泛的需求,但谷歌不会告诉我。

4

2 回答 2

5

我认为最接近规范的检查方法Autoconf 存档中的AX_LIB_SOCKET_NSL

# ===========================================================================
#        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])])
])
于 2009-11-01T07:00:39.360 回答
0

我不记得有任何完成的代码,但是您通常想检查是否可以先链接调用该gethostbyname()函数的程序而无需任何额外的库。仅当失败时,您才想尝试该nsl库。

类似的事情也适用于m图书馆。

于 2009-10-27T11:54:01.050 回答