前期准备
- Linux 系统,虚拟机、真机、子系统皆可
- NS3, 官方网站下载:https://www.nsnam.org/releases/
- Eclipse IDE for C/C++ Developers Linux 版本
本人使用的环境:
- Ubuntu 18.04 子系统/Bash on Windows (Windows 10)
- NS3 v3.29
- Eclipse Photon v4.8.0
搭建NS3环境
当下载好NS3后,可以在终端输入命令 ./build.py 进行构建。该命令成功执行后会在当前目录生成一个ns-3.29文件夹,该文件夹中就是我们通过命令构建的NS3,也是我们所需要的。
之后,我们再进入到生成的ns-3.29文件夹中,依次执行 ./waf configure — enable-examples — enable-tests 与 ./waf build 即可对NS3中的组件进行编译,编译成功后可以看到如下输出,说明NS3环境已经大丈夫(没问题)了😀。
Waf: Leaving directory `/home/near/ns3/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (20m56.899s)
Modules built:
antenna aodv applications
bridge buildings config-store
core csma csma-layout
dsdv dsr energy
fd-net-device flow-monitor internet
internet-apps lr-wpan lte
mesh mobility mpi
netanim (no Python) network nix-vector-routing
olsr point-to-point point-to-point-layout
propagation sixlowpan spectrum
stats tap-bridge test (no Python)
topology-read traffic-control uan
virtual-net-device wave wifi
wimax
Modules not built (see ns-3 tutorial for explanation):
brite click openflow
visualizer
Eclipse 的配置
新建NS3工程
打开Eclipse, 依次点击 File->New->Project,选择 C++ Project,然后 Project Type 选择 Executable -> Empty Project, Toolchains 选择 Linux GCC,然后直接点击Finish。具体配置见下图。

设置Waf为默认编译器
鼠标右键单击NS3工程,在选中的属性中选择 C/C++ Build, 需要配置Builder Settings 以及 Behaviour, 具体配置如下。


配置成功后可以在 Eclipse 的 Console 中看到NS3已经构建成功,因为之前我们在命令行中已经Build过了一次,所以这次会比较快,只用了5.590s 。

配置Eclipse Run Configuration
上一步我们把NS3在Eclipse编译的环境配置好了,那么只是能在Eclipse编译而已,如果我们需要在Eclipse中运行NS3的程序,就还需要进一步的配置。
选中标题栏 Run -> External Tools -> External Tools Configuration,需要配置的地方为Location, 设置为waf的地址,Working Directory 设置为NS3工程的地址,即waf的父目录,Arguments设置为 –run “$(string_prompt)”具体配置如下。

使用Eclipse 运行试试
点击应用并运行后,就可以在输入框中输入需要运行的程序,例如,scratch-simulator.cc 只需要在输入框中输入 scratch-simulator 即可,运行结果会在Console中显示。


这样就大功告成了。
附录
- 关于Bash on Windows的知识,可以查看本博客的另一篇文章。Bash on Windows:Windows平台下Ubuntu图形界面程序
- Eclipse IDE for C/C++ Developers Linux 版本下载链接:https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/2018-12/R/eclipse-cpp-2018-12-R-linux-gtk-x86_64.tar.gz&tdsourcetag=s_pctim_aiomsg