Chapter 8. C and C++ projects

Table of Contents

Common script for C/C++ applications
Library interaction (uselib)
Customizing object files
Using configuration headers
The include system
Configuration helpers
Pkg-config

Common script for C/C++ applications

The c/c++ builds consist in transforming source files into object files, and to assemble the object files at the end. In theory a single programming language should be sufficient for writing any application, but in practice slight variations exist:

  • Applications may be divided in dynamic or static libraries
  • Additional files may enter in the link step (libraries, object files)
  • Source files may be generated by other compilers

The construction of c/c++ applications can be quite complicated, and several measures must be taken to ensure coherent interaction with new compilation rules. The canonical code for a task generator building a c/c++ application is the following:

def build(bld):
	t = bld.new_task_gen(
		features     = 'cc cprogram', 1
		source       = 'main.c', 2
		target       = 'appname', 3
		includes     = '.', 4
		install_path = '${SOME_PATH}/bin', 5
		defines      = ['LINUX=1', 'BIDULE'], 6
		ccflags      = ['-O2', '-Wall'], 7
		lib          = ['m'], 8
		libpath      = ['/usr/lib']
	)
	if sys.platform != 'win32':
		t.rpath      = '/usr/lib' 9
			

1

Task generator declaration; each element in the list represent a feature; it is possible to add several languages at once (ocaml and c++ for example), but the one of cstaticlib, cshlib or cprogram must be chosen.

2

List of source, it may be either a python list, or a string containing the file names separated with spaces. This list may contain file names of different extensions to make hybrid applications.

3

Target name, it is concerted to the name of the binary name.so or name.exe depending on the platform and the features.

4

List of include paths, it may be either a python list, or a string containing the paths separated by spaces. The paths are used for both the command-line and for finding the implicit dependencies (headers). In general, include paths must be relative to the wscript file and given explicitly. See the section called “The include system”.

5

Installation directory, this is where to install the library or program produced. The ${} expression is a reference to a variable to be extracted from tgen.env. By default it is set to ${PREFIX}/bin for programs and ${PREFIX}/lib for libraries. To disable the installation, set it to None.

6

Command-line defines: list of defines to add to the command-line with the -D prefix. To reduce the size of the command-line, it is possible to use a configuration header, see the following section for more details.

7

Command-line compilation flags, for the c++ language the attribute is called cxxflags

8

Shared libraries may be given directly (use staticlib and staticlibpath for static libraries)

9

Additional pararameters may be added from a task generator reference. The next section describes a technique to gather the conditions into the configuration section.