Optimization of C++ extensions to Python
hi, I tried this question on the C++/Python SIG and was told to ask here. So. When using distutils to build an extension (shared library), a C++ library to be imported into python, I find the optimization level set for me, to -O2 This is great, I guess, but for certain debugging tools (valgrind) I need that to be -O0 I tried setting the extra_compile_args and get output like this gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.4 -c memrid.cpp -o build/temp.linux-i686-2.4/memrid.o -O0 Which as you can see, has -O2 (the default) following -DNDEBUG ... and my "extra" argument appended ... as -O0. I need to be sure that this overrides the -O2 setting (or know how to set it to -O0 correctly) ... I noted that the binaries produced are different and that the binary created when -O0 is in the gcc string is larger than the other... the GCC documentation I could find didn't address the issue of multiple flags on the same line and/or how the compiler would handle them, and I think there is/should be a "correct" way of doing this such that the confusion of "which optimization level is it using" doesn't exist. Can anyone a) tell me if my binary compiled w/extra_compile_args -O0 is, indeed, level zero b) tell me the correct way to adjust this flag? thank you -mike
Hi Michael, On Montag 30 Juni 2008, Michael Wieher wrote:
Can anyone a) tell me if my binary compiled w/extra_compile_args -O0 is, indeed, level zero b) tell me the correct way to adjust this flag?
Not sure about "correct", but this works for me: 8< --------------------------------------------------------- def hack_distutils(debug=False, fast_link=True): # hack distutils.sysconfig to eliminate debug flags # stolen from mpi4py def remove_prefixes(optlist, bad_prefixes): for bad_prefix in bad_prefixes: for i, flag in enumerate(optlist): if flag.startswith(bad_prefix): optlist.pop(i) break return optlist import sys if not sys.platform.lower().startswith("win"): from distutils import sysconfig cvars = sysconfig.get_config_vars() cflags = cvars.get('OPT') if cflags: cflags = remove_prefixes(cflags.split(), ['-g', '-O', '-Wstrict-prototypes', '-DNDEBUG']) if debug: cflags.append("-g") else: cflags.append("-O3") cflags.append("-DNDEBUG") cvars['OPT'] = str.join(' ', cflags) cvars["CFLAGS"] = cvars["BASECFLAGS"] + " " + cvars["OPT"] if fast_link: for varname in ["LDSHARED", "BLDSHARED"]: ldsharedflags = cvars.get(varname) if ldsharedflags: ldsharedflags = remove_prefixes(ldsharedflags.split(), ['-Wl,-O']) cvars[varname] = str.join(' ', ldsharedflags) 8< --------------------------------------------------------- Distutils may just be the most-monkeypatched piece of software in history. Andreas
participants (2)
-
Andreas Klöckner
-
Michael Wieher