如何使用waf构建共享库?
我想使用 waf 构建一个共享库,因为它看起来更容易并且比 GNU 自动工具更简洁。
到目前为止,我实际上有几个与我开始编写的 wscript 相关的问题:
VERSION='0.0.1'
APPNAME='libmylib'
srcdir = '.'
blddir = 'build'
def set_options(opt):
opt.tool_options('compiler_cc')
pass
def configure(conf):
conf.check_tool('compiler_cc')
conf.env.append_value('CCFLAGS', '-std=gnu99 -Wall -pedantic -ggdb')
def build(bld):
bld.new_task_gen(
features = 'cc cshlib',
source = '*.c',
target='libmylib')
包含 source = '*.c'
的行不起作用。我必须指定每个 .c 文件而不是使用通配符吗?
例如,如何启用调试构建(当前 wscript 正在使用调试构建 CFLAGS,但我希望最终用户可以选择此选项)。
计划将库源放在一个子目录中,并且使用 lib 的程序分别放在自己的子目录中。
I want to build a shared library using waf as it looks much easier and less cluttered than GNU autotools.
I actually have several questions so far related to the wscript I've started to write:
VERSION='0.0.1'
APPNAME='libmylib'
srcdir = '.'
blddir = 'build'
def set_options(opt):
opt.tool_options('compiler_cc')
pass
def configure(conf):
conf.check_tool('compiler_cc')
conf.env.append_value('CCFLAGS', '-std=gnu99 -Wall -pedantic -ggdb')
def build(bld):
bld.new_task_gen(
features = 'cc cshlib',
source = '*.c',
target='libmylib')
The line containing source = '*.c'
does not work. Must I specify each and every .c file instead of using a wildcard?
How can I enable a debug build for example (currently the wscript is using the debug builds CFLAGS, but I want to make this optional for the end user).
It is planned for the library sources to be within a sub directory, and programs that use the lib each in their own sub directories.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用的是最新版本的 waf(撰写本文时为 1.5.9),可以通过
glob()
构建上下文中的方法。因此,您可以编写以下内容:如果您使用的是没有 glob 的旧版本 waf,那么您可以使用一个方法
find_sources_in_dirs
:此方法仍在 Waf 中,但已预定弃用并可能最终消失。
srcdir
和blddir
变量现在是可选的,因此您不需要它们 - 它们默认为“.”。无论如何,“构建”。您不应在目标名称前添加“lib”,这是以特定于平台的方式自动完成的(在 Windows 上不添加 lib,共享库使用 .dll)。调试与发布构建是一个令人惊讶的棘手问题。最初 Waf 包含此功能,但在某个时候被删除并且从未重新添加。这是邮件列表上的常见请求,因此将来可能会重新出现。同时,您可能会做比使用 gjc 的 cflags 模块。只需将其添加到您的项目目录中即可。最终的 wscript 将是:要设置调试版本,您将运行以下命令:
如果您在自己的子目录中使用库,那么您可能应该有一个顶级 wscript 并使用
bld.add_subdirs()
添加库的技术/程序目录。每个子目录都有自己的 wscript_build 文件。然后,您可以使用export_incdirs
和uselib_local
属性指定库和程序“模块”之间的正确包含目录。Assuming you are using the latest version of waf (1.5.9 at the time of writing), wild cards can be specified via the
glob()
method on the build context. So you can write the following:If you were using an older version of waf that doesn't have glob, then there is a method
find_sources_in_dirs
that you can use:This method is still in Waf but is slated for deprecation and may eventually disappear.
The
srcdir
andblddir
variables are optional now so you don't need them - they default to "." and "build" anyway. You shouldn't prepend "lib" to the target name, this is done automatically in a platform specific way (on Windows no lib is added and shared libraries use .dll). Debug vs Release build is a surprisingly thorny issue. Originally Waf included this feature, but it was dropped at some point and never re-added. It's a common request on the mailing list so may resurface in the future. Meanwhile you could do a lot worse than use gjc's cflags module. Just add it to your project directory. The final wscript would then be:And to set up a debug build you would run the following:
If you are using libraries in their own sub-directories, then you should probably have a top level wscript and use the
bld.add_subdirs()
technique to add library/program directories. Each sub-directory would have its own wscript_build file. You can then use theexport_incdirs
anduselib_local
properties to specify the correct include directories between library and program "modules".多年来,waf 发生了很大变化,因此问题中的代码和答案都不再适用于当前的 waf。现在你只需编写:
请注意,waf 会自动添加
lib
前缀,因此你不必编写target
.在 Windows 上,您还必须将= '库名称'
defs
关键字参数添加到shlib
函数调用中。就我个人而言,我建议不要使用涉及
wscript_build
文件的递归构建脚本。并不是说它不起作用(如 递归 makefile),只是保留所有逻辑要简单得多在一个中等大小的构建脚本中。waf has changed a lot over the years so neither the code in the question nor the answer works with current waf anymore. Nowadays you just write:
Note that waf will automatically add the
lib
prefix so you don't writetarget
. On Windows, you also have to add the= 'libname'
defs
keyword argument to theshlib
function call.Personally I would recommend against recursive build scripts involving
wscript_build
files. Not that it doesn't work (like recursive makefiles), it's just much simpler to keep all logic in one medium-sized build script.