'Export variable from scons script to source file

I have an environment variable ( key value) pair in scons script. I want to export the value of this variable in a cpp source file.

How do I do that? Because scons scripts may not be available after compiling.

I have tried to do something using export but it's still not happening.

The value of the environment variable is a string.



Solution 1:[1]

You'll want to use env.Textfile()

See manpage entry here: https://scons.org/doc/production/HTML/scons-man.html#b-textfile

Example SConstruct

env=Environment(XYZ=2)
env.Textfile('outfile.txt', source=['int xyz=$XYZ;\n'], varlist=['XYZ'])

NOTE: whatever variables you use in source via $VARIABLE_NAME, you'll need to list in varlist, otherwise changing the variable(s) values won't cause outfile.txt to be rebuilt.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bdbaddog