Adding include path to Waf configuration (C++)
Asked Answered
B

3

11

How can I add a include path to wscript?

I know I can declare which files from which folders I want to include per any cpp file, like:

def build(bld):
    bld(features='c cxx cxxprogram',
        includes='include', 
        source='main.cpp', 
        target='app', 
        use=['M','mylib'], 
        lib=['dl'])

but I do not want to set it per every file. I want to add a path to "global includes" so it will be included everytime any file will be compiled.

Burn answered 10/2, 2013 at 19:8 Comment(0)
B
15

I've found an answer. You have to simply set the value of 'INCLUDES' to list of paths you want. Do not forget to run waf configure again :)

def configure(cfg):
    cfg.env.append_value('INCLUDES', ['include'])
Burn answered 10/2, 2013 at 21:19 Comment(1)
I converged on this answer independently, but I wandered here hoping there was a more OS independent way to do this.Norvin
G
3

I spent some time working out a good way to do this using the "use" option in bld.program() methods. Working with the boost libraries as an example, I came up with the following. I hope it helps!

'''
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')
Gates answered 14/2, 2013 at 17:4 Comment(0)
N
0

I figured this out and the steps are as follows:

Added following check in the configure function in wscript file. This tells the script to check for the given library file (libmongoclient in this case), and we store the results of this check in MONGOCLIENT.

conf.check_cfg(package='libmongoclient', args=['--cflags', '--libs'], uselib_store='MONGOCLIENT', mandatory=True)

After this step, we need to add a package configuration file (.pc) into /usr/local/lib/pkgconfig path. This is the file where we specify the paths to lib and headers. Pasting the content of this file below.

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}

Added the dependency into the build function of the sepcific program which depends on the above library (i.e. MongoClient). Below is an example.

mobility = bld( target='bin/mobility', features='cxx cxxprogram', source='src/main.cpp', use='mob-objects MONGOCLIENT', )

After this, run the configure again, and build your code.

Nikola answered 5/5, 2015 at 17:33 Comment(1)
In future, please tailor your answers to the question at hand, and not just duplicate the answer. Here, the answer does happen to apply to both questions, and the questions are different enough that you cannot dupe them (one asks for adding includes, the other adding a library). At least explain in each one how it applies to the specific situation in the question.Zipangu

© 2022 - 2024 — McMap. All rights reserved.