Re: How do I tell cython where to find my header file

384 views
Skip to first unread message

Saullo Castro

unread,
Jul 6, 2013, 4:03:44 PM7/6/13
to cython...@googlegroups.com
Add the two environment variables:

in a python IDE run:
import numpy
the_path = numpy.get_include()

keep "the_path" to configure  the two environment variables:

CPATH=the_path

CFLAGS=-Ithe_path

This should make it work...


On Sunday, November 11, 2012 12:00:51 AM UTC+1, Volker Jaenisch wrote:
Hi Cythonists!

I wrote a small c test extension that I like to embedd into python using cython.

utils.h  :
unsigned long long atoull (const char *str);

utils.c :
#include <string.h>

unsigned long long atoull (const char *str) {
   unsigned long long num,temp;
   int i,j;
   if (str == NULL) return 0;
  
   num = 0;
  
   for (i=strlen(str)-1; i>=0; i--) {
      temp = (str[i]-'0');
      for (j=strlen(str)-1; j>i; j--)
         temp *= 10;
      num += temp;
      }
   return num;
   }


setup.py :

utilsmodule = Extension('utils',
                    define_macros = [('MAJOR_VERSION', '1'),
                                     ('MINOR_VERSION', '0')],
                    include_dirs = ['./src/hamm/diagnose/getcan/'],
                    library_dirs = ['.'],
                    sources = ['./src/hamm/diagnose/getcan/utils.c'])
...
      ext_modules = [utilsmodule]
      )

The extension is builded without problems.

But if I try to use it from cython:

cdef extern from 'utils.h':
    cdef unsigned long long atoull (char* str)

I got the following "header file not found error" from gcc:

(hamm)volker@killer:~/workspace/hamm/getCAN$ ./bin/rinjcan
/home/volker/.pyxbld/temp.linux-x86_64-2.6/pyrex/hamm/diagnose/getcan/rinjcan.c:256:19: error: utils.h: No such file or directory
/home/volker/.pyxbld/temp.linux-x86_64-2.6/pyrex/hamm/diagnose/getcan/rinjcan.c: In function ‘__pyx_pf_4hamm_8diagnose_6getcan_7rinjcan_2main’:
/home/volker/.pyxbld/temp.linux-x86_64-2.6/pyrex/hamm/diagnose/getcan/rinjcan.c:2090: warning: implicit declaration of function ‘atoull’
Traceback (most recent call last):
  File "./bin/rinjcan", line 23, in <module>
    import hamm.diagnose.getcan.test
  File "/home/volker/workspace/hamm/getCAN/src/hamm/diagnose/getcan/test.py", line 2, in <module>
    import injcan, rinjcan
  File "/home/volker/workspace/hamm/getCAN/eggs/Cython-0.17.1-py2.6-linux-x86_64.egg/pyximport/pyximport.py", line 427, in load_module
    language_level=self.language_level)
  File "/home/volker/workspace/hamm/getCAN/eggs/Cython-0.17.1-py2.6-linux-x86_64.egg/pyximport/pyximport.py", line 209, in load_module
    inplace=build_inplace, language_level=language_level)
  File "/home/volker/workspace/hamm/getCAN/eggs/Cython-0.17.1-py2.6-linux-x86_64.egg/pyximport/pyximport.py", line 186, in build_module
    reload_support=pyxargs.reload_support)
  File "/home/volker/workspace/hamm/getCAN/eggs/Cython-0.17.1-py2.6-linux-x86_64.egg/pyximport/pyxbuild.py", line 104, in pyx_to_dll
    dist.run_commands()
  File "/usr/lib/python2.6/distutils/dist.py", line 975, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python2.6/distutils/dist.py", line 995, in run_command
    cmd_obj.run()
  File "/home/volker/workspace/hamm/getCAN/eggs/Cython-0.17.1-py2.6-linux-x86_64.egg/Cython/Distutils/build_ext.py", line 163, in run
    _build_ext.build_ext.run(self)
  File "/usr/lib/python2.6/distutils/command/build_ext.py", line 340, in run
    self.build_extensions()
  File "/home/volker/workspace/hamm/getCAN/eggs/Cython-0.17.1-py2.6-linux-x86_64.egg/Cython/Distutils/build_ext.py", line 171, in build_extensions
    self.build_extension(ext)
  File "/usr/lib/python2.6/distutils/command/build_ext.py", line 499, in build_extension
    depends=ext.depends)
  File "/usr/lib/python2.6/distutils/ccompiler.py", line 621, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "/usr/lib/python2.6/distutils/unixccompiler.py", line 180, in _compile
    raise CompileError, msg
ImportError: Building module hamm.diagnose.getcan.rinjcan failed: ["CompileError: command 'gcc' failed with exit status 1\n"]

The problem ist clear: Cython can simply not find the header file utils.h . How do I tell cython where to look for utils.h ?

Cheers,

Volker


Reply all
Reply to author
Forward
0 new messages