I want to make a simple example of calling C code from Go with CGO. But for some reason I can't achieve desired. Compilation fails with the following error:
go build main.go # awesomeProject1/print duplicate symbol '_do_print' in: $WORK/b002/_x002.o $WORK/b002/_x003.o ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) The code:
// print/print.c #include <stdio.h> void do_print(char * x){ printf("%s", x); } // print/print.go package print // #include <print.c> import "C" func DoPrint() { C.do_print(C.CString("Hello!")) } // main.go package main import "awesomeProject1/print" func main() { print.DoPrint() } If I make do_print function static it compiles but I wouldn't be able to do that for 3rd party code I want to integrate with later. Am I missing some essential piece from documentation? Tutorials are all alike and claim to work where my example fails. Please help! Go version 1.16.4
print.cgets compiled twice. Generally, including a.cfile is a bad idea.