一聚教程网:一个值得你收藏的教程网站

热门教程

CentOS 6 编译 libnl-3.2.25的步骤详解

时间:2022-06-30 21:05:34 编辑:袖梨 来源:一聚教程网

一、说话有一个软件编译的时候提示为 libnl3: no,所有需要安装一个 libnl,这里选择编译安装最新版 libnl-3.2.25.tar.gz,编译过程如下:

wget http://www.infradead.org/~tgr/libnl/files/libnl-3.2.25.tar.gz
tar zxvf libnl-3.2.25.tar.gz
cd libnl-3.2.25
./configure --prefix=/usr/
make && make install

二、执行 ./configure 后有如下提示:


-------------------------------------------------------------------------------
                                  NOTE
 
 There have been some changes starting with 3.2 regarding where and how libnl
 is being installed on the system in order to allow multiple libnl versions
 to be installed in parallel:
 
    - Headers will be installed in ${prefix}/include/libnl3, therefore
      you will need to add "-I/usr/include/libnl3" to CFLAGS
 
    - The library basename was renamed to libnl-3, i.e. the SO names become
      libnl-3.so., libnl-route-3.so, etc.
 
    - libtool versioning was assumed, to ease detection of compatible library
      versions.
 
 If you are using pkg-config for detecting and linking against the library
 things will continue magically as if nothing every happened. If you are
 linking manually you need to adapt your Makefiles or switch to using
 pkg-config files.
 
-------------------------------------------------------------------------------
三、再次编译那个软件的时候需要先执行如下命令,让编译能找到对应的lib

export LIBGNUTLS_LIBS="-L/usr/lib/ -lgnutls"
export LIBGNUTLS_CFLAGS="-I/usr/include/"
 
export LIBNL3_CFLAGS="-I/usr/include/libnl3"
export LIBNL3_LIBS="-L/usr/lib/ -lnl-3 -lnl-route-3"

四、libnl3 是什么?

核心库(core library)提供了使用 netlink 套接字进行通信的基础功能。它处理套接字的连接建立和断开、发送和接收数据、构造和解析消息、提供可配置的接收状态机。除此之外它还提供了一套抽象数据类型的框架,这套框架使得基于对象的 netlink 协议实现起来更加的简单,在这种协议中,对象可以通过基于 netlink 的协议来添加、删除、或者修改。

如何链接到这个库

使用 autoconf 检查库是否存在

那些使用 autoconf 的项目可以使用 PKG_CHECK_MODULES() 来检测系统中是否存在某个特定版本的 libnl。下面这个例子同时也展示了如何取得链接到 libnl 库所需要的 CFLAGS 和链接依赖。

下面这个例子展示 了如何检查特定版本的 libnl 是否存在。如果存在,这个例子也展示了如何正确的扩展 CFLAGS 和 LIBS 变量:

PKG_CHECK_MODULES(LIBNL3,libnl3-3.0>=3.1,[have_libnl3]=yes,[have_libnl3=no])
if (test "$(have_libnl3)"="yes"); then
 CFLAGS+="$(LIBNL3_CFLAGS)"
 LIBS+="$(LIBNL3_LIBS)"
fi
注意: pkgconfig 被命名成 libnl-3.0.pc 是遗留问题,它实际上也包含了版本号大于3.1 的库。

头文件

需要包含的头文件主要是 这个文件。根据你使用的子系统和组件的不同,你可能还需要在你头文件中添加一些额外的头文件。

#include
#include
#include
依赖于版本号的代码

如果你希望能在你的代码中链接 libnl 的多个版本,你可以让编译器根据你想要链接的libnl 库的特定版本来编译你代码中包含的特定部分的代码。

#include

#if LIBNL_VER_NUM >= LIBNL_VER(3.1)
 /* include code if compiled with libnl version >= 3.1 */
#end if
链接

$gcc myprogram.c -o myprogram $(pkgconfig --cflags --libs libnl-3.0)
1.3. 调试

这个库在编译的时候包含了调试语句,这使得它可以在你把 NLDBG 这个环境变量的值设置为> 0 的值的时候往 stderr 中打印调试信息。

$ NLDBG=2 ./myprogram
表 1. 调试级别
级别  描述
0

关闭调试 (默认)

1

警告信息、重要的事件和通知信息

2

多一些更不重要的信息

3

导致调试信息刷屏重复性事件

4

比上面的信息还更不重要的消息

Netlink 协议的调试

通常查看套接字之间交换的 netlink 消息流是非常有用的。把环境变量 NLCB 的值设置为debug(NLCB=debug)可以运行调试消息处理器,它会把交换的 netlink 消息打印成易于我们阅读的格式并输出到 stderr 上。

$ NLCB=debug ./myprogram
-- Debug: Sent Message:
--------------------------   BEGIN NETLINK MESSAGE ---------------------------
  [HEADER] 16 octets
    .nlmsg_len = 20
    .nlmsg_type = 18 <route/link::get>
    .nlmsg_flags = 773 <REQUEST,ACK,ROOT,MATCH>
    .nlmsg_seq = 1301410712
    .nlmsg_pid = 20014
  [PAYLOAD] 16 octets
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00       ................
---------------------------  END NETLINK MESSAGE   ---------------------------
-- Debug: Received Message:
--------------------------   BEGIN NETLINK MESSAGE ---------------------------
  [HEADER] 16 octets
    .nlmsg_len = 996
    .nlmsg_type = 16 <route/link::new>
    .nlmsg_flags = 2 <MULTI>
    .nlmsg_seq = 1301410712
    .nlmsg_pid = 20014
  [PAYLOAD] 16 octets
    00 00 04 03 01 00 00 00 49 00 01 00 00 00 00 00       ........I.......
  [ATTR 03] 3 octets
    6c 6f 00                                              lo.
  [PADDING] 1 octets
    00                                                    .
  [ATTR 13] 4 octets
    00 00 00 00                                           ....
  [ATTR 16] 1 octets
    00                                                    .
  [PADDING] 3 octets
    00 00 00                                              ...
  [ATTR 17] 1 octets
    00                                                    .
  [...]

热门栏目