Skip to content

Commit dace8a7

Browse files
committed
First commit, hello world for swig.
0 parents  commit dace8a7

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
I already had gcc and related stuff, so that might be a dependency.
2+
3+
after creating example.(i|c|h), I ran the following:
4+
5+
``` bash
6+
sudo apt-get install swig
7+
swig -python example.i
8+
```
9+
10+
Then created setup.py and ran that:
11+
12+
``` bash
13+
python setup.py build_ext --inplace
14+
```
15+
16+
and then could import:
17+
18+
``` python
19+
>>> import example
20+
>>> example.fact(4)
21+
24
22+
```

example.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* File: example.c */
2+
3+
#include "example.h"
4+
5+
int fact(int n) {
6+
if (n<0){ /* This should propably return an error, but this is simpler*/
7+
return 0;
8+
}
9+
if (n == 0) {
10+
return 1;
11+
}
12+
else {
13+
/* testing for overflow would be a good idea here */
14+
return n * fact(n-1);
15+
}
16+
}

example.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* File: example.h */
2+
3+
int fact(int n);

example.i

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* File example.i */
2+
%module example
3+
4+
%{
5+
#define SWIG_FILE_WITH_INIT
6+
#include "example.h"
7+
%}
8+
9+
int fact(int n);

setup.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
setup.py file for SWIG example
5+
"""
6+
7+
from distutils.core import setup, Extension
8+
9+
10+
example_module = Extension('_example',
11+
sources=['example_wrap.c', 'example.c'],
12+
)
13+
14+
setup (name = 'example',
15+
version = '0.1',
16+
author = "SWIG Docs",
17+
description = """Simple swig example from docs""",
18+
ext_modules = [example_module],
19+
py_modules = ["example"],
20+
)

0 commit comments

Comments
 (0)