I am trying to get a minimal example of using Go code in C to work, such as this. I am struggling with the following compilation error message:
ld: warning: ignoring file /Users/username/gocode/src/example/example.dylib, file was built for archive which is not the architecture being linked (x86_64): /Users/username/gocode/src/example/example.dylib
My attempt is the following. I have a simple Go package in a file main.go:
package example
import "C"
//export GoEcho
func GoEcho(s *C.char) string {
return C.GoString(s)
}
func main() {}
which I then compile using either
go build -buildmode=c-archive -o example.dylib main.go
or
go build -buildmode=c-shared -o example.dylib main.go
or
GOARCH=amd64 go build -buildmode=c-shared -o LibraryLinkExamples.dylib main.go
The operating system is OS X 10.11 and the the Go version is, as go version
puts it, go1.9 darwin/amd64
.
The C code I'm using is the following:
#include "example.h"
#include <stdio.h>
int main(int argc, char const *argv[])
{
GoString res = GoEcho("test");
printf("%.*s
", (int)res.n, res.p);
return 0;
}
I don't understand why I am getting the "which is not the architecture being linked (x86_64)" error message when I am building the library and using it on the same operating system, even the computer? What can explain this?
Update In the official documentation here it says:
amd64 (also known as x86-64)
meaning it should be x86-64 compatible with this setting. That it isn't is possibly a bug in go build
?