20

I am compiling some cython extensions in linux and noticed that it defaults to using -O2 when building from the distutils mechanism. I was wondering if there was a simple way to change that to a -O3.

I have tried using the extra_compile_args on Extension objects, but that leads to both -O2 and -O3 being passed as arguments to gcc. I kind of want to play with other esoteric gcc options and thus am hoping I can just control the compilation step. An obvious question is "why don't I just run cython my.pyx and compile the results manually?". I would love to, is my answer... but the cython executable in /usr/local/bin/ throws a DistributionNotFound: Cython==0.12.1 error when run from the command line. I haven't quite figured that one out.

Anyway, I am not sure if its a cython thing, a distutils thing or a broken apt package thing. I simply grabbed cython out of the ubuntu 11.10 apt repo (and am currently using ubuntu 11.10).

Voltaire
  • 1,375
  • 4
  • 17
  • 22
  • If you have Cython installed from apt, it shouldn't be in `/usr/local/bin`. Have you tried running `/usr/bin/cython` explicitly? – Fred Foo Nov 23 '11 at 21:03
  • That question came to my mind yesterday :) I didn't found clean answer appart for redoing a crossplatform compiler extension ... – tito Dec 02 '11 at 09:02
  • does [this answer](https://stackoverflow.com/a/68423687/12141949) helps with your problem? – 0dminnimda Jul 17 '21 at 19:31

2 Answers2

18

Using extra_compile_args=["-O3"] in your setup.py, the -O3 should appear after the -O2 option overriding it. Check the shared object (.so, or .dll) size in order to confirm it quickly.

Davide

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
Davide Albanese
  • 572
  • 5
  • 10
2

larsmans comment was right - using /usr/bin/cython addresses my issue.

Voltaire
  • 1,375
  • 4
  • 17
  • 22