cgo Compiling C files housed in different directory than Go files

148 views
Skip to first unread message

Palash B

unread,
Apr 14, 2023, 1:14:05 PM4/14/23
to golang-nuts

Hello folks.

I am working on writing an Interpreter (for my own programming language) which is written in C and I want to build a Go API for my interpreter.

My current directory structure is something like this

...
 cpank/*.c
cpank/stdlib/*.c
cpank/ext/*.c
cpank/ext/*.h
cpank/include/*.h
goapi/api.go
...

cpank/*.c contains the core files, stdlib contains source for standard library, ext curently contains 2 files xxhash.c and xxhash.h but can and will contain more files later.

I have tried putting the go file in main cpank directory but it fails. If I directly include the c files, cgo throws duplicate errors. Only thing that works is copying all c files from cpank, stdlib, ext and the include directory into the goapi directory.

Is there any way to tell cgo to compile this, this and that files like we use with normal c projects.

How do I make this work?

Any help or suggestions will be appreciated.

Jason E. Aten

unread,
Apr 15, 2023, 4:13:03 AM4/15/23
to golang-nuts
perhaps take a look at how the Go bindings for Lua work. https://github.com/aarzilli/golua

Jason E. Aten

unread,
Apr 15, 2023, 4:14:11 AM4/15/23
to golang-nuts

Palash Bauri

unread,
Apr 16, 2023, 4:14:48 AM4/16/23
to golan...@googlegroups.com
I have made it work, by putting a go file in every child directory.

https://github.com/bauripalash/cpank
Reply all
Reply to author
Forward
0 new messages