一、說話有一個軟體編譯的時候提示為 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 的庫。
標頭檔
需要包含的標頭檔主要是 <netlink/netlink.h> 這個檔案。根據你使用的子系統和組件的不同,你可能還需要在你標頭檔中添加一些額外的標頭檔。
#include <netlink/netlinl.h>
#include <netlink/cache.h>
#include <netlink/route/link.h>
依賴於版本號碼的代碼
如果你希望能在你的代碼中連結 libnl 的多個版本,你可以讓編譯器根據你想要連結的libnl 庫的特定版本來編譯你代碼中包含的特定部分的代碼。
#include <netlink/version.h>
#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 .
[...]