0

I tried to run the cython example on http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html I basically just copied the code in Rectangle.h, Rectangle.cpp, setup.py and rect.pyx However, when I run python setup.py build_ext --inplace I get the error

running build_ext
building 'rect' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c rect.c -o build/temp.linux-x86_64-2.7/rect.o
In file included from rect.c:235:0:
Rectangle.h:1:1: error: unknown type name ‘namespace’
Rectangle.h:1:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
rect.c:236:15: fatal error: ios: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

What am I doing wrong???

Jonathan Lindgren
  • 1,192
  • 3
  • 14
  • 31

2 Answers2

2

Rectangle.h:1:1: error: unknown type name ‘namespace’

namespace is only recognized by C++ compilers. I'm guessing you meant to use g++ instead of the gcc compiler. Change your build_ext to use g++ and also for the sake of clarity, rename your file to rectangle.cpp

Shashank
  • 13,713
  • 5
  • 37
  • 63
  • My file is called Rectangle.cpp? How do I change to g++? I tried to add --compiler=g++ as an argument when running setup.py but I get error: don't know how to compile C/C++ code on platform 'posix' with 'g++' compiler – Jonathan Lindgren Sep 13 '13 at 19:38
  • 1
    Your file is called Rectangle.cpp? How to explain this from your error message then? `In file included from rect.c:235:0:` – john Sep 13 '13 at 19:41
  • @JonathanLindgren See this question: http://stackoverflow.com/questions/16737260/how-to-tell-distutils-to-use-gcc In `setup.py` you should set environment variables with `os.environ` to tell which compiler to use. – Shashank Sep 13 '13 at 19:42
  • the rect.c is probably a file generated by cython, so its nothing I created (called .c because I used the wrong compiler as pointed out here). Now it seems like it works, thanks! (but I must say its really bad this was not explained in the cython/c++ tutorial, or maybe I missed it somewhere?) – Jonathan Lindgren Sep 13 '13 at 19:52
1

In your setup.py script set the language to c++ in ext_modules

...
ext_modules=[
    Extension("rect",
    sources=["rect.pyx"],
    language="c++",
    )]

setup(
  name = 'rect',
  ext_modules = cythonize(ext_modules),
)

Cython will now call the correct c++ compiler

Bryce Guinta
  • 3,456
  • 1
  • 35
  • 36