I have a project written entirely in C. I just need to call one function which will have to be coded in GO. Therefore I am using cgo to build C files and then I would like to use the resulting object files to link with my main C project.
Assume I have a single file foo.go
with a single function func HelloWorld()
. I wish to execute this function from my C code.
So I do the following steps
# go tool cgo foo.go
# cd _obj && gcc -c *.o
Now I have two files:
# _cgo_export.o _cgo_main.o
However, main()
is getting somehow defined in there and I cannot link these object files with my main project (multiple mains).
How do I get cgo to not put in a dummy main? Or am I doing this entirely wrong?
EDIT I need the entry point to be in C. The suggestions others have posted require the entry point to be in GO. I have posted the correct solution below, thanks to another user whom I have credited.