环境:
一、下载ns-allinone-2.34。
原版本下载:点此下载。
二、安装X Windows的开发包和g++编译器
在终端输入如下命令:
如果还是没有成功安装的话,可以先安装这些软件包。
sudo apt-get install build-essential
sudo apt-get install tcl8.4 tcl8.4-dev tk8.4 tk8.4-dev
sudo apt-get install libxmu-dev libxmu-headers
sudo apt-get install xorg-dev g++xgraph
sudo apt-get install g++-4.4
三、修改otcl的configure文件
然后解压 ns-allinone-2.34.tar.gz,放到你指定的目录。检查你的gcc版本,如果gcc版本大于4.0(大部分人的都是,建议直接改),要在执行./install之前作出一些修改。
gcc4.0版本以前是用ld-share来生成共享库的,但是到了4.0以上的版本,这个命令改为了gcc-share。
修改命令如下:
cdns-allinone-2.34/otcl-1.13
sudogedit configure.in 把 77行处的 SHLIB_LD="ld-shared" 改为 SHLIB_LD="gcc-shared"保存退出,然后
sudogedit configure 把 6304行(Ctrl+i跳到6304行)的 SHLIB_LD="ld-shared" 改为 SHLIB_LD="gcc-shared" 保存退出,然后
在终端中cd 到ns-allinone-2.34目录下,然后输入 ./install ,进行安装
laycher@ubuntu:~/ns-allinone-2.34$ ./instal
四、出现问题补充
=======================================================================================
tools/ranvar.cc: In member function ‘virtual doubleGammaRandomVariable::value()’:
tools/ranvar.cc:219:70: error: cannot call constructor‘GammaRandomVariable::GammaRandomVariable’ directly
tools/ranvar.cc:219:70:error:
make: *** [tools/ranvar.o]错误1
Ns make failed!
See http://www.isi.edu/nsnam/ns/ns-problems.html for problems
即:将219行的
return GammaRandomVariable::GammaRandomVariable(1.0 + alpha_,beta_).value() * pow (u, 1.0 / alpha_);
改为:
return GammaRandomVariable(1.0 + alpha_, beta_).value() * pow(u, 1.0 / alpha_);
======================================================================================
错误二:
In fileincluded from mac/mac-802_11Ext.cc:66:0:
mac/mac-802_11Ext.h: In member function‘u_int32_t PHY_MIBExt::getHdrLen11()’:
mac/mac-802_11Ext.h:175:19: error: expectedprimary-expression before ‘struct’
mac/mac-802_11Ext.h:175:41: error: ‘dh_body’ wasnot declared in this scope
mac/mac-802_11Ext.h:175:51: error: ‘offsetof’ wasnot declared in this scope
mac/mac-802_11Ext.h:177:3: warning: controlreaches end of non-void function [-Wreturn-type]
make: ***[mac/mac-802_11Ext.o] Error 1
Ns makefailed!
下面是网上找得关于本问题的解决方案:
If you get error like:
mac/mac-802_11Ext.h: In member function ‘u_int32_tPHY_MIBExt::getHdrLen11()’:
mac/mac-802_11Ext.h:176:19: error: expected primary-expressionbefore ‘struct’
mac/mac-802_11Ext.h:176:41: error: ‘dh_body’ was not declared inthis scope
mac/mac-802_11Ext.h:176:51: error: ‘offsetof’ was not declared inthis scope
open that file and add
#include <cstddef>
to the header files.
在ns-allinone-2.34\ns-2.34\mac\mac-802_11Ext.h文件添加#include<cstddef>
然后重新安装,就OK了。
=============================================================================
错误三:
mobile/nakagami.cc: In member function ‘virtual doubleNakagami::Pr(PacketStamp*, PacketStamp*, WirelessPhy*)’:
mobile/nakagami.cc:183:73: error: cannot call constructor‘ErlangRandomVariable::ErlangRandomVariable’ directly
mobile/nakagami.cc:183:73:error:
mobile/nakagami.cc:185:67: error: cannot call constructor‘GammaRandomVariable::GammaRandomVariable’ directly
mobile/nakagami.cc:185:67:error:
make: *** [mobile/nakagami.o]错误1
Ns make failed!
See http://www.isi.edu/nsnam/ns/ns-problems.html forproblems
解决方法:
在ns-allinone-2.34/ ns-2.34/mobile文件夹下,找到报错提示中的nakagami.cc文件,打开找到对应的183行删除::ErlangRandomVariable,保存,
即:将183行的
resultPower = ErlangRandomVariable::ErlangRandomVariable(Pr/m,int_m).value();
改为:
resultPower = ErlangRandomVariable(Pr/m, int_m).value();
在ns-allinone-2.34/ ns-2.34/mobile文件夹下,找到报错提示中的nakagami.cc文件,打开找到对应的185行删除::GammaRandomVariable,保存,
即:将185行的
resultPower = GammaRandomVariable::GammaRandomVariable(m,Pr/m).value();
改为:
resultPower = GammaRandomVariable(m, Pr/m).value();
错误四:
In file included from linkstate/ls.cc:67:0:
linkstate/ls.h: In instantiation of ‘void LsMap::eraseAll() [with Key = int; T = LsIdSeq]’:
linkstate/ls.cc:396:28: required from here
linkstate/ls.h:137:20: error: ‘erase’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
linkstate/ls.h:137:20: note: declarations in dependent base ‘std::map, std::allocator > >’ are not found by unqualified lookup
linkstate/ls.h:137:20: note: use ‘this->erase’ instead
make: *** [linkstate/ls.o] Error 1
Ns make failed!
修改ls文件:ns-2.35/linkstate/ls.h
第137行
void eraseAll() { erase(baseMap::begin(), baseMap::end()); }
改为:void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); }
重新在ns目录下键入$./install安装,再次出现同类问题时,仿照此次解决方法,找到对应的文件和行数,修改即可。直到安装成功。
=======================================================================================
五、设置全局路径变量。
安装成功后,会出现如下图所示的文字,该部分提示你必须设置三个全局变量PATH、LD_LIBRARY_PATH、TCL_LIBRARY。
laycher@ubuntu:~/ns-allinone-2.34$ cd (回到home目录下)
laycher@ubuntu:~$ vim .bashrc (用vim编辑器编辑文件.bashrc,如果没有vim,可以用gedit或者vi)
在该文件的最后一行下插入如下字段。
export PATH=$PATH:~/ns-allinone-2.34/bin:/home/laycher/ns-allinone-2.34/tcl8.4.18/unix:/home/laycher/ns-allinone-2.34/tk8.4.18/unix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/laycher/ns-allinone-2.34/otcl-1.13:/home/laycher/ns-allinone-2.34/lib
export TCL_LIBRARY=$TCL_LIBRARY:/home/laycher/ns-alllinone-2.34/tcl8.4.18/library
每一句export为一行,共三行,export和PATH之间有一个空格,后面部分不要用任何字符隔开。每个路径用“:”隔开。“~”相当于/home/laycher。上面的laycher用户名改为自己的。如果安装ns低版本的,请自行修改相应的版本号。
(1)打开一个新的终端
(2)输入ns并回车
$ns (如果正常,会出现"%"操作提示符,如果显示还没有安装ns2,可以先安装nam,再测试ns)
(3)输入一段测试用的Tcl脚本代码进行测试
%puts "Hello World" (输出Hello World字符串)
Hello World (如果正确,会显示Hello World)
% (然后跳到下一个"%"提示符等待下一条指令输入)
============================
安装nam
cd ns-allinone-2.34/nam-1.14
./configure
make
sudo make install (至此,nam安装好)
到此ns2终于安装好!!