Another go question, this time related to cgo and linking, which I haven't been able to find a ton of info about on the internets.
So, say that I'm writing C++ and I want to make a static library. My static library might call outside functions that aren't included in it and will be linked in later, but that's fine, because static libraries aren't linked.
mylib.cpp:
Code:
void funcDefinedElsewhere();
void myLibFunc() {
funcDefinedElsewhere();
}
Code:
$ clang -c mylib.cpp
$ ar -r libmylib.a mylib.o
Tada, I have libmylib.a. If I try to link it into a shared library or executable that doesn't define funcDefinedElsewhere(), there will be a linker error, but that's the behavior I'm looking for here.
Now, what if I want to do the same thing with Go and
build modes?
Code:
package main
// #include "test.h"
import "C"
func goFunc() {
C.myFunc()
}
func main() {}
Whoops, can't do that:
Code:
$ go build -buildmode=c-archive
Undefined symbols for architecture x86_64:
"_myFunc", referenced from:
__cgo_c45d7460d1f4_Cfunc_myFunc in test.cgo2.o
(maybe you meant: __cgo_c45d7460d1f4_Cfunc_myFunc)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Okay, so Go will always try to link calls into C code. What if I add a .c file that defines the function
Go is trying to call (myFunc), and the
C code makes a call to an external library that will be linked in later on?
Nope:
Code:
$ go build -buildmode=c-archive
Undefined symbols for architecture x86_64:
"_externalFunc", referenced from:
_myFunc in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is there any way to do this? Why does Go insist on everything being linked in static libraries?