Search code examples
pythoncompiler-flags

how to "export CFLAGS='my -flags -here' from python script


I'm writing a python program that needs to set the environment CFLAGS as needed.

I'm using the subprocess module to perform some operations, but, I'm not sure this is the correct way of doing this.

The script will first set the CFLAGS and then compile some code, so the cflags need to stay put while the code is compiled.

I know there is the os.environ['CXXFLAGS'] which defaults to "" in my system. So my question is, do I just need to set the os.environ['CXXFLAGS'] value before compiling the code, or do I need to do it some other way?

Please advise


Solution

  • Setting it in the environment by modifying os.environ['CXXFLAGS'] should work. However, the way I've always passed extra CXXFLAGS to ./configure is by passing it on the command line, e.g.:

    cmd = [
        './configure',
        'CXXFLAGS=-O2 -march=i586 -mtune=i686',
    ]
    subprocess.Popen(cmd)
    

    When done this way, you shouldn't need to set CXXFLAGS in the environment or explicitly pass it to make (autotools will create the Makefiles so that they include your custom CXXFLAGS).