Im trying to call a simple c function from within go with cgo
The files are as follows:
goFile.go:
package main
//#include "cFile.h"
import "C"
func main() {
C.printInC()
}
cFile.h:
void printInC();
cFile.c:
#include "cFile.h"
#include <stdio.h>
void printInC(){
printf("Test");
}
running go build goFile.go throws the following exception:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: $WORK\b001\_x002.o: in function `_cgo_f9774dcf54b4_Cfunc_printInC':
/tmp/go-build/cgo-gcc-prolog:49: undefined reference to `printInC'
collect2.exe: error: ld returned 1 exit status
I'm not really sure why this isn't working, I've looked at multiple tutorials for cgo that implement calling c functions exactly the same way without a problem.
import "C". The compiler flags and linking information may also be mentioned if required..cfile which is automatically referenced. It's just like C and C++ where the header file is included. But, you may also write your C or C++ code e.g. functions in the comments and reference those later in Go code. Here's an example.