方式一:这种方式需要的内存空间比较大(我的笔记本不行,但是服务器可以)
官方网站:ns-3 Installation Guide — Installation guide
下载:
git clone https://gitlab.com/nsnam/ns-3-dev.git
cd ns-3-dev
切换分支:
git checkout -b ns-3.39-branch ns-3.39
编译:
./ns3 configure --enable-examples --enable-tests
./ns3 build
方式二:这种方式内存需求不大,在笔记本上关闭不用的程序,可编译成功,编译过程中遇到内存出错的话,重新编译就行
根据这个视频安装的:
ns-3网络模拟中文入门视频教程02-安装_哔哩哔哩_bilibili
下载来源:
解压:
tar jxf ns-allinone-3.43.tar.bz2
cd ns-allinone-3.43
编译:(./build --help可以查看帮助信息)
./build.py --enable-examples --enable-tests
运行示例:
cd ns-3.43
./ns3 run hello-simulator
下面是一个调用ns3动态链接库运行的例子:
代码:
// mysimple.cc
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("MyNS3App");
int main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
// 创建两个节点
NodeContainer nodes;
nodes.Create (2);
// 配置点对点链路
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
// 安装协议栈
InternetStackHelper stack;
stack.Install (nodes);
// 配置 IP 地址
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// 在节点1上安装 UDP Echo Server
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// 在节点0上安装 UDP Echo Client
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
CMakelists.txt
cmake_minimum_required(VERSION 3.16)
project(MyNS3App)
# 设置 C++ 标准
#set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# 设置 NS-3 安装路径(请根据实际路径修改)
set(NS3_DIR "/home/jjg/codes/ns-allinone-3.43/ns-3.43/build")
# 添加头文件路径
include_directories(${NS3_DIR})
include_directories(${NS3_DIR}/include)
# 查找 NS-3 的共享库(假设已经用 ./ns3 configure --enable-examples --enable-tests --build-shared 开启 shared)
link_directories(${NS3_DIR}/lib)
# 要使用的 NS-3 模块列表(根据需要添加)
#set(NS3_MODULES
# ns3-core
# ns3-network
# ns3-internet
# ns3-csma
# ns3-point-to-point
# ns3-applications
#)
# 源文件
add_executable(my_ns3_app myapp.cc)
# 链接共享库
#target_link_libraries(my_ns3_app ${NS3_MODULES})
target_link_libraries(my_ns3_app PRIVATE
"${NS3_DIR}/lib/libns3.43-core-default.so"
"${NS3_DIR}/lib/libns3.43-network-default.so"
"${NS3_DIR}/lib/libns3.43-internet-default.so"
"${NS3_DIR}/lib/libns3.43-csma-default.so"
"${NS3_DIR}/lib/libns3.43-point-to-point-default.so"
"${NS3_DIR}/lib/libns3.43-applications-default.so"
)