0

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?

EDIT: I am trying to compile to ppc using the gccgo, and I don't want to use cross-compilation process of go compiler. I have tried to do (as suggested in the comments):

go build -compiler=gccgo

and it worked. Thou, when I do:

go build -comiler=powerpc-linux-gnu-gccgo main.go

I get:

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.
TomE8
  • 546
  • 1
  • 4
  • 14
  • 1
    [This](https://forum.golangbridge.org/t/go-import-work-with-cgo-not-with-gccgo/5176)? I mean, does it work if you use `go build -compiler=gccgo`? – kostix Aug 26 '20 at 16:09
  • Do you know the explain what does it do? what is the difference between that and calling directly to gccgo? – TomE8 Aug 26 '20 at 16:21
  • `cgo` is a `go` tool, so you need to use `go` to generate the `C` package. – JimB Aug 26 '20 at 18:04
  • thank you, that helped. I have edit the question since it is more complex then I thought at the beginning – TomE8 Aug 26 '20 at 19:39
  • What @JimB, said, yes: interfacing Go with C is a complex task which is carried out by the dedicated subsystem called [`cgo`](https://golang.org/cmd/cgo/). – kostix Aug 27 '20 at 12:54
  • The edit to your original question had actually made it another question completely ;-) The thing is, the `-compiler` option to `go` is not the name of the compiler binary but rather the name of a supported "suite", of which there are two: `gc` (the built-in, stock one) and `gccgo`. What you probably need is to use appropriate environment variables to tell `gcc` which cross-compiler to use. Please read carefully [this document](https://golang.org/cmd/cgo/). – kostix Aug 27 '20 at 13:00
  • Regarding the `-compile` option, it is documented [here](https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies). – kostix Aug 27 '20 at 13:03

0 Answers0