Skip to content

Commit dfe4041

Browse files
author
jarod
committed
Simple demonstration of generating swig bindings for a C++ class for guile
0 parents  commit dfe4041

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.so
2+
*.o

example.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "example.h"
2+
3+
MyClass::MyClass(int initialValue) : value(initialValue) {}
4+
5+
int MyClass::getValue() const {
6+
return value;
7+
}
8+
9+
void MyClass::setValue(int newValue) {
10+
value = newValue;
11+
}
12+
13+
extern "C" {
14+
MyClass* create_object(int initialValue) {
15+
return new MyClass(initialValue);
16+
}
17+
18+
int get_value(MyClass* obj) {
19+
return obj->getValue();
20+
}
21+
22+
void set_value(MyClass* obj, int newValue) {
23+
obj->setValue(newValue);
24+
}
25+
26+
void delete_object(MyClass* obj) {
27+
delete obj;
28+
}
29+
}

example.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// example.hpp
2+
class MyClass {
3+
public:
4+
MyClass(int initialValue);
5+
int getValue() const;
6+
void setValue(int newValue);
7+
private:
8+
int value;
9+
};

example.i

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
%module example
2+
%{
3+
#include "example.h"
4+
%}
5+
6+
%include "example.h"

example.scm

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; example.scm
2+
;(use-modules (system foreign) (system foreign-library) (ice-9 ftw))
3+
4+
;(define libexample (dynamic-link "./libexample.so"))
5+
6+
;(define create-object (foreign-library-function libexample "create_object"
7+
; #:return-type intptr_t
8+
; #:arg-types int))
9+
;(define get-value (foreign-library-function libexample "get_value"
10+
; #:return-type int
11+
; #:arg-types intptr_t))
12+
;(define set-value (foreign-library-function libexample "set_value"
13+
; #:return-type void
14+
; #:arg-types (intptr_t int))
15+
;(define delete-object (foreign-library-function libexample "delete_object"
16+
; #:return-type void
17+
; #:arg-types intptr_t))
18+
19+
;(define create-object (foreign-library-function libexample "create_object"))
20+
;(define get-value (foreign-library-function libexample "get_value"))
21+
;(define set-value (foreign-library-function libexample "set_value"))
22+
;(define delete-object (foreign-library-function libexample "delete_object"))
23+
24+
;(define (cleanup)
25+
; (dynamic-unlink libexample))
26+
(load-extension "./libexample.so" "SWIG_init")
27+
28+
; Example of using the C++ code from Scheme
29+
(display "Creating C++ object from Scheme: ")
30+
(let* ((obj (new-MyClass 42))
31+
(initial-value (MyClass-getValue obj)))
32+
(display "Initial value: ")
33+
(display initial-value)
34+
(newline)
35+
36+
(display "Setting new value from Scheme: ")
37+
(MyClass-setValue obj 99)
38+
(display "New value: ")
39+
(display (MyClass-getValue obj))
40+
(newline)
41+
42+
(delete-MyClass obj)) ; Clean up the C++ object
43+
44+
; Cleanup - close the shared library
45+
;(cleanup)

instructions.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
swig -c++ -guile example.i
2+
3+
g++ -fPIC -I/usr/include/guile/3.0 -c example_wrap.cxx
4+
g++ -fPIC -c example.cpp
5+
g++ -shared example.o example_wrap.o -o libexample.so

0 commit comments

Comments
 (0)