pythonc++boostswig

Setting compiler flags using swig and python


I have problems with including boost into my c++ code, which is complied using 'swig'. I want to c++ as a backend to my python stuff.

Calling these two commands

swig -c++ -python spherical_overlap.i
python setup.py build_ext --inplace

the latter gives me the following error

clang: warning: -lboost_system : 'linker' input unused
In file included from spherical_overlap_wrap.cxx:3427:
./spherical_overlap.h:8:10: fatal error: 'boost/math/special_functions/bessel.hpp' file not found
#include <boost/math/special_functions/bessel.hpp>

The file is located there. I think I have to set the following flag for the compiler

-I /usr/local/include

The problem is, I do not know how to do that. Here is my 'setup.py' file

#!/usr/bin/env python

from distutils.core import setup, Extension


spherical_overlap_module = Extension('_spherical_overlap',
                           sources=['spherical_overlap_wrap.cxx', 'spherical_overlap.cpp'],
                           swig_opts=['-c++', '-py3'],
                           extra_compile_args =['-lboost_system '],
                           )

setup (name = 'spherical_overlap',
       version = '0.1',
       author      = "SWIG Docs",
       description = """Simple swig spherical_overlap from docs""",
       ext_modules = [spherical_overlap_module],
       py_modules = ["spherical_overlap"],
       )

Solution

  • You can do either of the following:

    1. Add include_dirs = ['/usr/local/include'], to the Extension constructor call

    2. Add the following to a file setup.cfg:

      [build_ext]
      include-dirs = /usr/local/include
      
    3. Specify the include-dirs option to the build_ext command, i.e., run

      python setup.py build_ext --inplace --include-dirs /usr/local/include
      
    4. Add -I/usr/local/include to the CPPFLAGS environment variable, e.g., run

      CPPFLAGS="-I/usr/local/include" python setup.py build_ext --inplace
      

    Probably the second is the preferred way, since it reflects options that depends on the local system configuration.