I am trying to build a C-archive from a Go-file with an external dependency on a C-function. I try to use the following command
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build -buildmode=c-archive -o demo.a demo.go
The problem I encounter is that the go build command demands that foo() is defined. Am I fundamentally missing something regarding how Go works? How do I build a static library with dependencies?
The error from go build
Undefined symbols for architecture x86_64:
"_foo", referenced from:
__cgo_849fe01848a8_Cfunc_foo in demo.cgo2.o
(maybe you meant: __cgo_849fe01848a8_Cfunc_foo)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
My demo.go looks like the following
package demo
/*
extern int foo();
*/
import "C"
import (
"fmt"
)
//export afunc
func afunc() {
C.foo()
fmt.Println("Hello from afunc")
}
I am aware that I can get around this using function pointers, but I would still like to know why a step in the go build process demands that the function in question is defined when building a static library.
Any ideas?