5

I'm new to Cython and I'm trying to compile Cython from this project without success.

With this setup.py,

from distutils.core import setup, Extension
from Cython.Distutils import build_ext
from distutils.extension import Extension

sources_list = ["timgraph.pyx", "Graph.cpp", "InfGraph.cpp", "sfmt/SFMT.c"]

setup(ext_modules=[Extension("pytim",
                             sources=sources_list,
                             language="c++",
                             extra_compile_args=["-std=c++11"])
                  ],
      cmdclass={'build_ext':build_ext})

I run the following:

python setup.py build_ext --inplace

and get the following error:

error: invalid argument '-std=c++11' not allowed with 'C/ObjC'
error: command 'clang' failed with exit status 1

I'm running macOS High Sierra 10.13.2, Python 3.6.2, Cython 0.27.3, and Apple LLVM version 9.0.0, in case any of that helps.

EDIT: I thought maybe it's from trying to compile both C and C++ simultaneously, because I can run the Cython example for C++ and it works fine. But I don't know how to get around the fact that the extra_compile_args applies to all of the sources, including "sfmt/SFMT.c".

Community
  • 1
  • 1
chirpchirp
  • 121
  • 2
  • 8
  • `Clang` is a C compiler; specifying a version of the C++ standard is a bad idea. Either use `Clang++` or specify `-std=c11`. – Jonathan Leffler Feb 27 '18 at 03:59
  • @JonathanLeffler If I specify -std=c11, then it comes back with `error: invalid argument '-std=c11' not allowed with 'C++/ObjC++'`. How do I specify Clang++? – chirpchirp Feb 27 '18 at 04:12
  • Make sure you use `Clang++`. The files end `.cpp` so compile with a C++ compiler. – Jonathan Leffler Feb 27 '18 at 04:14
  • @JonathanLeffler Cython doesn't ask for a compiler, it just does it automatically, so I don't know how to specify the compiler for the .cpp files separately. Besides, I ran the [Cython example](http://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html) for C++ and it works just fine without specifying a C++ compiler, so that doesn't sound right to me. – chirpchirp Feb 27 '18 at 04:19
  • Fine. I know no more than you about Cynthia compilation. Your problem is related to passing C++ options and source files to a mainly C compiler. I don’t know what is different between the C++ example you say works and this example which doesn’t, but clearly there’s a crucial difference that you need to discover. Good luck, and have fun! (Maybe you should consider rewriting the code in the C file in C++?) – Jonathan Leffler Feb 27 '18 at 04:59
  • @JonathanLeffler The C file "SFMT.c" is an official Mersenne twister, so I'm not even close to qualified to rewrite that code. – chirpchirp Feb 27 '18 at 05:07
  • If you can switch to gcc it will build because `-std=c++11` for c-files is only a warning in gcc (not an error as with clang). If you are not going to change the c-file you always can build it as a static library and link it. I never saw that somebody used different flags for different languages with distutils, but maybe there is a way... – ead Feb 27 '18 at 05:15
  • I guess life is tough, sometimes. There are Mersenne Twisters in C++; indeed, you should check the C++11 library and the `` header — the template `mersenne_twister_engine` and its 32-bit (`mt19937`) and 64-bit (`mt19937_64`) implementations should get you to where you need to go. Assuming the Clang actually supports them, which is highly probable. – Jonathan Leffler Feb 27 '18 at 05:15
  • 1
    @ead Apparently not, with gcc I still get `error: invalid argument '-std=c++11' not allowed with 'C/ObjC'` and `error: command 'gcc' failed with exit status 1` – chirpchirp Feb 27 '18 at 05:27

2 Answers2

1

Just for the record, the solution was very simple: remove the extra_compile_args argument completely, but still set the language argument as c++, i.e.

from distutils.core import setup, Extension
from Cython.Distutils import build_ext
from distutils.extension import Extension

sources_list = ["timgraph.pyx", "Graph.cpp", "InfGraph.cpp", "sfmt/SFMT.c"]

setup(ext_modules=[Extension("pytim",
                         sources=sources_list,
                         language="c++")
                   ],
      cmdclass={'build_ext':build_ext})

This for whatever reason successfully compiles both the C and the C++.

chirpchirp
  • 121
  • 2
  • 8
  • 2
    probably because for your current compiler version `-std=c++11` is default. – ead Dec 01 '18 at 19:34
  • This doesn't solve the problem of passing different compiler flags to the C and C++ compilers – kwsp Jun 09 '23 at 16:02
0

Got the same issues with Python 3.7 and 3.6.5 and laters...

I got an error when I tried to run:

pip install python-crfsuite

That was derived from the setup.py file there...

The error:

error: invalid argument '-std=c99' not allowed with 'C++/ObjC++' 

The only thing that worked for me was to run it with Python 3.6.4

Go to here to install 3.6.4 and try again.

Or do it with Conda/Anaconda here

If you are facing issues with pkgs that you had on Python3.7 and you have mac try solving it with:

python location on mac osx

Kohn1001
  • 3,507
  • 1
  • 24
  • 26
  • Just FYI, this was unnecessary, as I got it working with Python 3.6.2. Your problem seems to have been a little different, and the error didn't come while pip installing. – chirpchirp Dec 01 '18 at 17:17