在Waf配置中添加包含路径 (C++)
我该如何为wscript添加一个包含路径呢?
我知道我可以在每个cpp文件中声明我想要包含哪些文件,来自哪个文件夹,比如:
def build(bld):
bld(features='c cxx cxxprogram',
includes='include',
source='main.cpp',
target='app',
use=['M','mylib'],
lib=['dl'])
但我不想在每个文件里都设置一次。我想添加一个“全局包含”的路径,这样每次编译任何文件时都会自动包含这个路径下的文件。
3 个回答
0
我搞明白了,步骤如下:
在wscript文件的配置函数里加了一个检查。这一步是告诉脚本去检查指定的库文件(这里是libmongoclient),然后把检查的结果存储在MONGOCLIENT里。
conf.check_cfg(package='libmongoclient', args=['--cflags', '--libs'], uselib_store='MONGOCLIENT', mandatory=True)
完成这一步后,我们需要在/usr/local/lib/pkgconfig路径下添加一个包配置文件(.pc)。这个文件里会指定库和头文件的路径。下面是这个文件的内容。
prefix=/usr/local
libdir=/usr/local/lib
includedir=/usr/local/include/mongo
Name: libmongoclient
Description: Mongodb C++ driver
Version: 0.2
Libs: -L${libdir} -lmongoclient
Cflags: -I${includedir}
然后在依赖于上述库的特定程序的构建函数里添加这个依赖(也就是MongoClient)。下面是一个例子。
mobility = bld( target='bin/mobility', features='cxx cxxprogram', source='src/main.cpp', use='mob-objects MONGOCLIENT', )
接下来,再次运行配置,然后构建你的代码。
3
我花了一些时间研究如何使用bld.program()方法中的“use”选项来做到这一点。以boost库为例,我想出了以下方法。希望对你有帮助!
'''
run waf with -v option and look at the command line arguments given
to the compiler for the three cases.
you may need to include the boost tool into waf to test this script.
'''
def options(opt):
opt.load('compiler_cxx boost')
def configure(cfg):
cfg.load('compiler_cxx boost')
cfg.check_boost()
cfg.env.DEFINES_BOOST = ['NDEBUG']
### the following line would be very convenient
### cfg.env.USE_MYCONFIG = ['BOOST']
### but this works too:
def copy_config(cfg, name, new_name):
i = '_'+name
o = '_'+new_name
l = len(i)
d = {}
for key in cfg.env.keys():
if key[-l:] == i:
d[key.replace(i,o)] = cfg.env[key]
cfg.env.update(d)
copy_config(cfg, 'BOOST', 'MYCONFIG')
# now modify the new env/configuration
# this adds the appropriate "boost_" to the beginning
# of the library and the "-mt" to the end if needed
cfg.env.LIB_MYCONFIG = cfg.boost_get_libs('filesystem system')[-1]
def build(bld):
# basic boost (no libraries)
bld.program(target='test-boost2', source='test-boost.cpp',
use='BOOST')
# myconfig: boost with two libraries
bld.program(target='test-boost', source='test-boost.cpp',
use='MYCONFIG')
# warning:
# notice the NDEBUG shows up twice in the compilation
# because MYCONFIG already includes everything in BOOST
bld.program(target='test-boost3', source='test-boost.cpp',
use='BOOST MYCONFIG')
15
我找到了答案。你只需要把'INCLUDES'的值设置为你想要的路径列表。
别忘了再运行一下 waf configure
哦 :)
def configure(cfg):
cfg.env.append_value('INCLUDES', ['include'])