如何使用waf构建共享库? [英] How do I use waf to build a shared library?

查看:112
本文介绍了如何使用waf构建共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 waf 来构建共享库比GNU自动工具更轻松,更混乱.

I want to build a shared library using waf as it looks much easier and less cluttered than GNU autotools.

到目前为止,实际上我有几个问题与我开始写的wscript有关:

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')

包含source = '*.c'的行不起作用.我必须指定每个.c文件而不使用通配符吗?

The line containing source = '*.c' does not work. Must I specify each and every .c file instead of using a wildcard?

例如,如何启用调试版本(当前wscript使用的是调试版本CFLAGS,但我想让最终用户将此选项设置为可选).

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).

计划将库源文件放在一个子目录中,而使用lib的程序则各自位于自己的子目录中.

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.

推荐答案

假定您使用的是最新版本的waf(在撰写本文时为1.5.9),则可通过

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:

bld.new_task_gen(
    features = 'cc cshlib',
    source = bld.glob('*.c'),
    target='mylib')

如果您使用的旧版本的waf没有glob,则可以使用方法find_sources_in_dirs:

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:

lib = bld.new_task_gen(
    features = 'cc cshlib',
    target = 'mylib')
lib.find_sources_in_dirs('.')

此方法仍在Waf中,但已计划弃用,并可能最终消失.

This method is still in Waf but is slated for deprecation and may eventually disappear.

srcdirblddir变量现在是可选的,因此您不需要它们-它们默认为".和构建".您不应在目标名称前加上"lib",这是通过平台特定的方式自动完成的(在Windows上未添加任何lib,共享库使用.dll).调试与发布版本是一个令人惊讶的棘手问题.最初,Waf包含此功能,但在某些时候已将其删除,并且从未重新添加.这是邮件列表上的常见请求,因此将来可能会重新出现.同时,您可能比使用 gjc的cflags模块.只需将其添加到您的项目目录中即可.最终的wscript将是:

The srcdir and blddir 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:

VERSION='0.0.1'
APPNAME='mylib'

def set_options(opt):
    opt.tool_options('compiler_cc')
    opt.tool_options('cflags', tooldir='.')

def configure(conf):
    conf.check_tool('compiler_cc')
    conf.check_tool('cflags', tooldir='.')

def build(bld):
    bld.new_task_gen(
        features = 'cc cshlib',
        source = bld.glob('*.c'),
        target=APPNAME)

要设置调试版本,您将运行以下命令:

And to set up a debug build you would run the following:

./waf configure -d debug

如果您在各自的子目录中使用库,则可能应该具有顶级wscript并使用 export_incdirs

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 the export_incdirs and uselib_local properties to specify the correct include directories between library and program "modules".

这篇关于如何使用waf构建共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆