tsorn
tsorn

Reputation: 3625

Compiling cython with gcc: No such file or directory from #include "ios"

Given a file docprep.pyx as simple as

from spacy.structs cimport TokenC

print("loading")

And trying to cythonize it via

cythonize -3 -i docprep.pyx

I get the following error message

docprep.c:613:10: fatal error: ios: No such file or directory
 #include "ios"
          ^~~~~
compilation terminated

As you can tell from the paths, this system has an Anaconda installation with Python 3.7. numpy, spacy and cython are all installed through conda.

Upvotes: 12

Views: 7934

Answers (3)

Ryan Kuhl
Ryan Kuhl

Reputation: 11

I know this is an old post but I just ran across this as well when trying to install a python package on MacOS 15.1.1 that used cython under the hood. I had installed commandline-tools on my mac without installing xcode which ended up being the problem. Once I installed xcode and ran sudo xcode-select --reset locally the ios headers were immediately available.

A good check would be that you get the following output:

xcode-select --print-path
>>> /Applications/Xcode.app/Contents/Developer

Hope this helps the next person!

Upvotes: 1

ead
ead

Reputation: 34347

<ios> is a c++-header. The error message shows that you try to compile a C++-code as C-code.

Per default, Cython will produce a file with extension *.c, which will be interpreted as C-code by the compiler later on.

Cython can also produce a file with the right file-extension for c++, i.e. *.cpp. And there are multiple ways to trigger this behavior:

  • adding # distutils: language = c++ at the beginning of the pyx-file.
  • adding language="c++" to the Extension definition in the setup.py-file.
  • call cython with option --cplus.
  • in IPython, calling %%cython magic with -+, i.e. %%cython -+.
  • for alternatives when building with pyximport, see this SO-question.

Actually, for cythonize there is no command line option to trigger c++-generation, thus the first options looks like the best way to go:

# distutils: language = c++

from spacy.structs cimport TokenC
print("loading") 

The problem is that spacy/structs.pxd uses c++-constructs, for example vectors or anything else cimported from libcpp:

...
from libcpp.vector cimport vector
...

and thus also c++-libraries/headers are needed for the build.

Upvotes: 9

Dielson Sales
Dielson Sales

Reputation: 1737

In my case, it worked using @mountrix tip, just add the language="c++" to your setup.py, an example:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy


extensions = [
    Extension("processing_module", sources=["processing_module.pyx"], include_dirs=[numpy.get_include()], extra_compile_args=["-O3"], language="c++")
]

setup(
    name="processing_module",
    ext_modules = cythonize(extensions),
)

Upvotes: 11

Related Questions