Painkiller
Painkiller

Reputation: 125

undefined reference to c function when calling from go code

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.

Upvotes: 1

Views: 2597

Answers (1)

Painkiller
Painkiller

Reputation: 125

I've found the problem.

When running go build goFile.go the go tool apparently only builds goFile.go, when i run go build on the entire directory everything works fine..

Upvotes: 3

Related Questions