I am creating a python module that is implemented in C++. I am using SWIG to create the interface. There are various ways to create the extension, I'm using the "preferred approach," which is via python's distutils and which is described here. The name of my module is "ParseEvents," and to compile it I run the following two commands:
swig -c++ -python ParseEvents.i
python setup.py build_ext --inplace
The first command creates a file ParseEvents_wrap.cxx
The second command uses the following setup.py file:
from distutils.core import setup, Extension
ParseEvents_module = Extension('_ParseEvents',
sources=['ParseEvents_wrap.cxx',],
extra_compile_args=["-Wno-deprecated","-O3"],
)
setup (name = 'ParseEvents',
ext_modules = [ParseEvents_module,],
py_modules = ["ParseEvents"]
)
Question: Where and how do I specify that I want my C++ code to be compiled with the -O3 compiler tag? I guessed that it would just be in the "extra_compile_args" part of the setup.py file, but that doesn't seem to be the case. When I run the second command (python setup.py build_ext --inplace), here's the output:
running build_ext
building '_ParseEvents' extension
creating build
creating build/temp.linux-x86_64-2.6
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.4 -c ParseEvents_wrap.cxx -o build/temp.linux-x86_64-2.4/Pa开发者_StackOverflowrseEvents_wrap.o -Wno-deprecated -O3
c++ -pthread -shared build/temp.linux-x86_64-2.4/ParseEvents_wrap.o -o _ParseEvents.so
Note that both the -O2 and -O3 flags are present in the second to last line in the output---I'd like to remove the -O2.
The GCC doc explicitly says:
http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Optimize-Options.html
If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.
This means your code will be compiled with -O3 in effect, just as you want it. No need to bother for duplicate optimization flags.
Distutils has the lovely feature of providing all the same flags that Python was compiled with. The result is that adding extra flags is easy, but removing them is a total pain. Doing so involves subclassing the compiler class, catching the arguments and manually removing the offending flag from the argument list used by the compile function. That's the theory anyway, the docs are too poor to actually guide you through what you have to do to make that happen.
But like Luther said, in your case the extra -O2
doesn't hurt anything.
精彩评论