2009-10-27 54 views
2

在Solaris上,编译使用套接字的程序时,需要将其与-lnsl -lsocket链接。许多这样的程序最初是为Linux编写的(不需要额外的库),因此不要在它们的配置脚本中检查这些库,尽管这是一个相当简单的添加。像这样(未经测试):在Solaris网络库中创建autoconf链接的规范方法是什么?

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

是否有规范的方式来做这个检查?甚至可能包含在autoconf发行版中?你可以想象这种需求相当普遍,但谷歌不会告诉我。

回答

5

我认为最接近一个规范的方法来检查这个是从Autoconf ArchiveAX_LIB_SOCKET_NSL macro

# =========================================================================== 
#  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 <[email protected]> 
# Copyright (c) 2008 Stepan Kasal <[email protected]> 
# Copyright (c) 2008 Warren Young <[email protected]> 
# 
# 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])]) 
]) 
+0

谢谢,这就是我正在寻找的。我没有意识到Autoconf存档:) – legoscia 2009-11-02 15:49:26

0

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

类似的东西适用于例如为m库。

+0

是的,这就是后面使用'AC_SEARCH_LIBS'的想法:它首先尝试查找功能,无需任何库,然后依次与列表中的每个库进行交互。 (在我的代码中,虽然这两个列表都包含单个库。) – legoscia 2009-10-27 12:08:41

相关问题