开发者

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

开发者 https://www.devze.com 2022-12-15 12:28 出处:网络
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

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


You can do this without modifying the python process's environment.

# Make a copy of the environment and modify that.
myenv = dict(os.environ)
myenv["CXXFLAGS"] = "-DFOO"

# Pass the modified environment to the subprocess.
subprocess.check_call(["make", "install"], env=myenv)

See the documentation for Python's subprocess module.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号