前言
本人也是NS3新手,由于需要添加外部库zmq,参考了很多网上资料,一开始无从下手,到抓瞎到处尝试,不太理解就按自己的理解的方法去尝试,最后终于添加成功。看了很多教程,有利用create-moudle.py重新生成模块的,然后改动wscript文件的,也有直接改动ns3下面的wscript文件的,两种方法都尝试过,第一种失败,第二种有很多讲法,最终尝试多次链接成功,比较简单。
下面列一些自己参考的资料,如果以下教程不适用,可以自己参考以下资料琢磨(需要翻墙)
How to integrate external libraries into ns3 using waf build system? (google.com)
HOWTO use ns-3 with other libraries - Nsnam
shieldroute: Extending NS3 with your module and extra libraries
How to link ns3 with external libraries? - YouTube
Linking ZMQ to a c++ program in my scratch folder (google.com)
Making use of external libraries in NS3 | Jiazi’s Home Page (jiaziyi.com)
Linking shared libraries in waf (google.com)
How to integrate external libraries into ns3 using waf build system? (google.com)
本人使用ns3.28,配置外部库需要用waf工具,下面是一些资料
官方网站 https://waf.io/
官方文档 https://waf.io/book/
API 参考 https://waf.io/apidocs/
一、下载安装外部库
以本人安装的zmq库为例,
参考链接:
https://blog.csdn.net/weixin_44843859/article/details/109508422?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-4-109508422-blog-116665382.pc_relevant_3mothn_strategy_recovery&spm=1001.2101.3001.4242.3&utm_relevant_index=7
https://zhuanlan.zhihu.com/p/398730348
https://zhuanlan.zhihu.com/p/398730348
1, 安装必须的库
sudo apt-get install libtool
sudo apt-get install pkg-config
sudo apt-get install build-essential
sudo apt-get install autoconf
sudo apt-get install automake
2、安装libsodium (加密库,如果需要使用ZMQ4的安全功能需要在安装ZMQ前安装它,并在ZMQ编译时添加–with-libsodium 也可以 --without-libsodium屏蔽该功能,通过不安装也能使用zmq)
git clone git://github.com/jedisct1/libsodium.git
git checout 1.0.10 //这里回退了几个版本,因为版本过高编译zmq时会出现-Werror=deprecated-declarations 错误,具体见zmq编译
cd libsodium
./autogen.sh
./configure && make check
sudo make install
sudo ldconfig
cd ../
3,下载、编译、安装libzmq
# 下载
git clone https://github.com/zeromq/libzmq.git
cd libzmq
# 查看tag
git tag
# 版本 获取指定的版本,不要⽤主分⽀,可能有bug
git checkout v4.3.2
./autogen.sh
./configure && make check
sudo make install
sudo ldconfig
cd ..
4,测试
client.c
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
//编译:gcc -o hwclient hwclient.c -lzmq
int main (void)
{
printf ("Connecting to hello world server...\n");
void *context = zmq_ctx_new (); // 一个线程
// 连接至服务端的套接字
void *requester = zmq_socket (context, ZMQ_REQ); // 内部创建了2个线程,总共变成了3个线程
zmq_connect (requester, "tcp://localhost:5555"); // 连接并绑定 绑定对应的io线程
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
char buffer [10];
printf ("A 正在发送 Hello %d...\n", request_nbr);
zmq_send (requester, "Hello", 5, 0);
zmq_recv (requester, buffer, 5, 0); // 收到响应才能再发
//printf ("接收到 Hello World %d\n", request_nbr);
printf ("B 正在发送 xiaoluo %d...\n", request_nbr);
//zmq_send (requester, "Darren", 6, 0); // 无效
//zmq_send (requester, "king", 4, 0); // 无效
zmq_recv (requester, buffer, 6, 0); // 收到响应才能再发
printf ("接收到xiaoluo World %d\n", request_nbr);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}
server.c
//
// Hello World 服务端
// 绑定一个REP套接字至tcp://*:5555
// 从客户端接收Hello,并应答World
//
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
//gcc -o hwserver hwserver.c -lzmq
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
// 与客户端通信的套接字
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555"); // 服务器要做绑定
assert (rc == 0);
while (1) {
// 等待客户端请求
char buffer [10];
int size = zmq_recv (responder, buffer, 10, 0);
buffer[size] = '\0';
printf ("收到 %s\n", buffer);
sleep (1); // Do some 'work'
// 返回应答
zmq_send (responder, "World", 5, 0);
}
return 0;
}
client编译:gcc -o client client.c -lzmq
server编译:gcc -o server server.c -lzmq
二、NS3配置
1,在ns3.28/wscript里,在def configure 函数里加入以下代码
#Add extenal lib zmq
conf.env.append_value('INCLUDES', '/usr/local/include')
# conf.env.append_value('LINKFLAGS', '-lzmq' )
conf.env.append_value("LINKFLAGS", ["-lzmq","-L/usr/local/lib/libzmq.a"])
# conf.env.append_value("LIB", ["Geographic"])
#仅添加上面代码并不行,会报出找不到函数的未定义错误
#添加这些后,突然就成功了哈哈哈哈
conf.env.append_value("LINKFLAGS", ["-L/usr/local/lib"]) #链接库地址
conf.env.append_value('CXXFLAGS', '-I/usr/local/include/zmq') #头文件地址
conf.env.append_value("LIB", ["zmq"]) #用于列出链接库列表
2,重新配置并编译
./waf clean //为避免配置失误可以先./waf clean 一下
CXXFLAGS="-Wno-error" ./waf configure //前缀CXXFLAGS="-Wno-error" 表明忽略一切警告,因为ns3会把所有警告当错误报告然后中止编译
./waf build
3,zmq的示例代码可以成功运行了~