SWIG tutorial

586 views
Skip to first unread message

Greg Worley

unread,
Oct 11, 2010, 1:22:20 PM10/11/10
to golang-nuts
Using two files: example.c and example.i
/* File : example.c */

#include <time.h>
double My_variable = 3.0;

int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}

int my_mod(int x, int y) {
return (x%y);
}

char *get_time()
{
time_t ltime;
time(&ltime);
return ctime(&ltime);
}

______________________________________________

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();

________________________________________________________

Then, according to the instructions in http://www.swig.org/Doc2.0/Go.html#Go

% swig -go example.i
% gcc -c -fpic example.c
% gcc -c -fpic example_wrap.c
% gcc -shared example.o example_wrap.o -o example.so
% 6g example.go
% 6c example_gc.c
example_gc.c:14 6c: No such file or directory: runtime.h

Am I missing something? - it's throwing: example_gc.c:14 6c: No such
file or directory: runtime.h

Ian Lance Taylor

unread,
Oct 11, 2010, 1:46:18 PM10/11/10
to Greg Worley, golang-nuts
Greg Worley <gr...@worleyworks.com> writes:

> % 6c example_gc.c
> example_gc.c:14 6c: No such file or directory: runtime.h
>
> Am I missing something? - it's throwing: example_gc.c:14 6c: No such
> file or directory: runtime.h

You probably need to set GOROOT in the environment. 6g has gotten smart
enough that it remembers GOROOT from when you compiled, but I don't
think that is true for 6c.

Ian

Greg Worley

unread,
Oct 11, 2010, 2:30:36 PM10/11/10
to golang-nuts
I have the following already set:

export GOROOT=$HOME/go
export GOARCH=amd64
export GOOS=linux
export GOBIN=$HOME/bin
export PATH=~/bin:"${PATH}"

all.bash is in $HOME/go/src



On Oct 11, 1:46 pm, Ian Lance Taylor <i...@google.com> wrote:

Greg Worley

unread,
Oct 11, 2010, 2:39:20 PM10/11/10
to golang-nuts
additionally
% which 6c
/home/richardworley/bin/6c

Russ Cox

unread,
Oct 11, 2010, 2:54:32 PM10/11/10
to Ian Lance Taylor, Greg Worley, golang-nuts
>> % 6c example_gc.c
>> example_gc.c:14 6c: No such file or directory: runtime.h
>>
>> Am I missing something? - it's throwing: example_gc.c:14 6c: No such
>> file or directory: runtime.h

You need to pass a -I option so that 6c can find runtime.h.
Typically this is -I ${GOROOT}/pkg/${GOOS}_${GOARCH}.

Russ

Ian Lance Taylor

unread,
Oct 11, 2010, 2:58:56 PM10/11/10
to Greg Worley, golang-nuts
Greg Worley <gr...@worleyworks.com> writes:

> I have the following already set:

Hmmm, perhaps you do need the explicit -I option after all for 6c. It
would be -I $${GOROOT}/pkg/$${GOOS}_$${GOARCH}.

For 64bit you also currently need -D_64BIT with 6c.

I plan to get back to this after my current set of fires.

Ian

Reply all
Reply to author
Forward
0 new messages