Gccgo can't compile cgo

I am trying to compile a simple go program using gccgo. My code uses cgo thou, gccgo couldn’t compile it. This is my code (which compiles using go compiler):

package main

// #include <stdio.h>
// #include <stdlib.h>
//
// static void myprint(char* s) {
//   printf("%s\n", s);
// }
import "C"
import "fmt"
func main(){
    fmt.Println("Start")
    cs := C.CString("Print from C")
    C.myprint(cs)
    fmt.Println("End")
}

when I compile the code using gccgo main.go -o gccoutput I get:

main.go:9:9: error: import file ‘C’ not found
import “C”
^
main.go:13:8: error: reference to undefined name ‘C’
cs := C.CString(“Print from C”)
^
main.go:14:2: error: reference to undefined name ‘C’
C.myprint(cs)
^

any ideas how to solve this?

1 Like
$ cat tom.go
package main

// #include <stdio.h>
// #include <stdlib.h>
//
// static void myprint(char* s) {
//   printf("%s\n", s);
// }
import "C"
import "fmt"

func main() {
	fmt.Println("Start")
	cs := C.CString("Print from C")
	C.myprint(cs)
	fmt.Println("End")
}

go:

$ go version
go version devel +758ac371ab Tue Aug 25 21:15:43 2020 +0000 linux/amd64

$ go run tom.go
Start
Print from C
End

gccgo:

$ gccgo --version
gccgo (Ubuntu 10-20200411-0ubuntu1) 10.0.1 20200411 (experimental) [master revision bb87d5cc77d:75961caccb7:f883c46b4877f637e0fa5025b4d6b5c9040ec566]

$ go run -compiler=gccgo tom.go
Start
Print from C
End
$

Can you explain what the -compiler mean?
I have tried to do:
go build -compiler=powerpc-linux-gnu-gccgo main.go
and got:

invalid value “powerpc-linux-gnu-gccgo” for flag -compiler: unknown compiler “powerpc-linux-gnu-gccgo”
usage: go build [-o output] [-i] [build flags] [packages]
Run ‘go help build’ for details.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.