项目的大致目录结构
$ tree
|-- src
| `-- wscript
`-- wscript
主目录下有一个主的wscript脚本,要编译的子目录src目录下也有一个wscript脚本(每个wscript是python脚本)
- 主目录的wscript内容
#! /usr/bin/env python
# encoding: utf-8
// 这里的注释应该是井号的
// 说明: 设置top目录为当前路径,输出路径为当前目录的build目录
top = '.'
out = 'build'
def configure(ctx):
print(' -> configureing the project in ' + ctx.path.abspath())
def ping(ctx):
print('-> ping from ' + ctx.path.abspath())
ctx.recurse('src')
小tip:最前面这行是使用python编译这个脚本的意思。就象有些bash脚本里面是#!bash 表示这是命令行的脚本文件一样。
- src目录下的wscript内容
#! /usr/bin/env python
#encoding: utf-8
def ping(ctx):
print('-> ping from ' + ctx.path.abspath())
./waf 函数讲解
(1) 首先执行
$ waf configure
Setting top to : /home/fcx/share/test/waf_learn/simple_test
Setting out to : /home/fcx/share/test/waf_learn/simple_test/build
-> configureing the project in /home/fcx/share/test/waf_learn/simple_test
'configure' finished successfully (0.036s)
于是在新建的build目录会生成一下配置文件
|-- build
| |-- c4che
| | |-- build.config.py
| | `-- _cache.py
| `-- config.log
|--.lock-wafbuild
|-- src
| `-- wscript
`-- wscript
其中配置数据存储在build/c4che/目录下,命令行选项和环境变量存储在build.config.py中。用户的配置设置存储在_cache.py中。
(2)然后执行:
$ waf ping
-> ping from /home/fcx/share/test/waf_learn/simple_test
-> ping from /home/fcx/share/test/waf_learn/simple_test/src
'ping' finished successfully (0.002s)
(3)waf dist命令
创建一个发布压缩包,包含源代码目录中的所有文件。默认将会把项目代码打包为一个名为noname-1.0.tar.bz2的压缩包,如果要自己命名,则在主wscript脚本的top命令前添加如:
#! /usr/bin/env python
#encoding: utf-8
APPNAME = 'waf_learn'
VERSION = '1.0.0'
top = '.'
out = 'build'
def configure(ctx):
print(' -> configureing the project in ' + ctx.path.abspath())
def ping(ctx):
print('-> ping from ' + ctx.path.abspath())
ctx.recurse('src')
这样生成的压缩包名为:waf_learn-1.0.0.tar.bz2
(4)waf distclean 命令
如果执行 waf distclean 将会删除build的目录。distclean函数不需要在主wscript脚本中定义,如果你自己定义了该函数,则默认的distclean函数调用将会被你定义的函数覆盖。
更多的可以见这个blog https://blog.youkuaiyun.com/rical730/article/details/71514404
默认配置
有一个文件记录了默认的配置信息,可以修改它的默认参数,即默认的是激活examples and tests 还是锁死:
文件名叫utils/ .ns3rc文件 $ vim .ns3rc
- 禁止:
#Set this equal to true if you want examples to be run.
examples_enabled = False
#Set this equal to true if you want tests to be run.
tests_enabled = False
- 允许:
#Set this equal to true if you want examples to be run.
examples_enabled = True
#Set this equal to true if you want tests to be run.
tests_enabled = True
运行
./waf --run yourprogram //yourprogram.cc
./waf --pyrun yourprogram.py
补充notice
cc文件不要加.cc后缀 不然反而报错
debug
./waf shell
./waf --run yourProgram --command-template=“gdb %s”
e.g.
./waf --run examples/tcp/tcp-star-server --command-template=“gdb %s”
编译好的可执行文件放在…/debug/example/…相应的目录里,自己可以查看到
更多waf相关资源
- 这个blog https://www.cnblogs.com/Lvkun/archive/2012/03/30/trans-waf-tutorial.html 部分waf的翻译
- waf book的官网 https://waf.io/book/