Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

the building code was not working (it did not have -fPIC flag) so I updated it based on swig man page.
Source Link

The quickest way to do this is using SWIG.

Example from SWIG tutorial:

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on Unix:

swig -python example.i
gcc -fPIC -c example.c example_wrap.c \
  -I/usr/local/include/python2.17
ldgcc -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

Note that you have to have python-dev. Also in some systems python header files will be in /usr/include/python2.7 based on the way you have installed it.

From the tutorial:

SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language — exposing the underlying functionality in a very natural manner.

The quickest way to do this is using SWIG.

Example from SWIG tutorial:

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on Unix:

swig -python example.i
gcc -c example.c example_wrap.c \
  -I/usr/local/include/python2.1
ld -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

From the tutorial:

SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language — exposing the underlying functionality in a very natural manner.

The quickest way to do this is using SWIG.

Example from SWIG tutorial:

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on Unix:

swig -python example.i
gcc -fPIC -c example.c example_wrap.c -I/usr/local/include/python2.7
gcc -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

Note that you have to have python-dev. Also in some systems python header files will be in /usr/include/python2.7 based on the way you have installed it.

From the tutorial:

SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language — exposing the underlying functionality in a very natural manner.

Improved formatting
Source Link
Palec
  • 13.8k
  • 8
  • 80
  • 146

The quickest way to do this is using SWIG.

Example from swigSWIG tutorial:

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on unixUnix:

swig -python example.i
gcc -c example.c example_wrap.c \
 -I/usr/local/include/python2.1
ld -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

From the tutorial: "SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language---exposing the underlying functionality in a very natural manner".

SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language — exposing the underlying functionality in a very natural manner.

The quickest way to do this is using SWIG.

Example from swig tutorial:

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on unix:

swig -python example.i
gcc -c example.c example_wrap.c \
 -I/usr/local/include/python2.1
ld -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

From the tutorial: "SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language---exposing the underlying functionality in a very natural manner".

The quickest way to do this is using SWIG.

Example from SWIG tutorial:

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on Unix:

swig -python example.i
gcc -c example.c example_wrap.c \
 -I/usr/local/include/python2.1
ld -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

From the tutorial:

SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language — exposing the underlying functionality in a very natural manner.

added an example of Swig usage, as suggested in the comment. This improves the answer and follows the edit guidelines.
Source Link

The quickest way to do this is using SWIG.

Example from swig tutorial :

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on unix:

swig -python example.i
gcc -c example.c example_wrap.c \
 -I/usr/local/include/python2.1
ld -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

From the tutorial: "SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language---exposing the underlying functionality in a very natural manner".

The quickest way to do this is using SWIG.

The quickest way to do this is using SWIG.

Example from swig tutorial :

/* File : example.c */
int fact(int n) {
 if (n <= 1) return 1;
 else return n*fact(n-1);
}

Interface file:

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern int fact(int n);
%}
extern int fact(int n);

Building a Python module on unix:

swig -python example.i
gcc -c example.c example_wrap.c \
 -I/usr/local/include/python2.1
ld -shared example.o example_wrap.o -o _example.so

Usage:

>>> import example
>>> example.fact(5)
120

From the tutorial: "SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language---exposing the underlying functionality in a very natural manner".

Source Link
Ben Hoffstein
  • 103.6k
  • 8
  • 107
  • 121
Loading
default

AltStyle によって変換されたページ (->オリジナル) /