Library interaction (uselib)

To link a library against another one created in the same Waf project, the attribute uselib_local may be used. The include paths, the link path and the library name are automatically exported, and the dependent binary is recompiled when the library changes:

def build(bld):
	staticlib = bld.new_task_gen(
		features       = 'cc cstaticlib', 1
		source         = 'test_staticlib.c',
		target         = 'teststaticlib',
		export_incdirs = '.') 2

	main = bld.new_task_gen(
		features       = 'cc cprogram', 3
		source         = 'main.c',
		target         = 'test_c_program',
		includes       = '.',
		uselib_local   = 'teststaticlib') 4
			

1

A static library

2

Include paths to export for use with uselib_local (include paths are not added automatically). These folders are taken relatively to the current target.

3

A program using the static library declared previously

4

A list of references to existing libraries declared in the project (either a python list or a string containing the names space-separated)

To link an application against various system libraries, several compilation flags and link flags must be given at once. To reduce the maintenance, a system called uselib can be used to give all the flags at the same time:

def configure(conf):
	conf.env['CCFLAGS_TEST'] = '-O2' 1
	conf.env['LINKFLAGS_TEST'] = '-g'

def build(bld):
	staticlib = bld.new_task_gen(
		features = 'cc cstaticlib',
		source   = 'test_staticlib.c',
		target   = 'teststaticlib',
		uselib   = 'TEST') 2
			

1

Declare a few variables during the configuration, the variables follow the convention VAR_NAME

2

Add all the VAR_NAME corresponding to the uselib NAME, which is 'TEST' in this example

The variables used for c/c++ are the following: STATICLIB, LIB, LIBPATH, LINKFLAGS, RPATH, CXXFLAGS, CCFLAGS, CPPPATH, CPPFLAGS, CXXDEFINES, FRAMEWORK, FRAMEWORKPATH, CXXDEPS. The uselib is similar to the cascading style sheet (CSS) principle.