Go build go tool: no such tool "cgo"

hei, when i go build i got message

go tool: no such tool “cgo”

when i check the GOTOOLDIR iusing go env s /usr/local/lib/gccgo/tool.
i check cgo in that directory, cgo is not found

gccgo is not in go release.
You should install or build gccgo separately.

https://golang.org/doc/install/gccgo#Source_code

I’ve installed gccgo, but I can’t go build, I checked cgo, I found cgo in the directory :

/usr/local/pkg/tool/linux_arm

and my GOTOOLDIR is /usr/local/ lib/gccgo, there is no cgo in it

I think that you can run gccgo regardless of cgo.

package main

import "fmt"

func main() {
        fmt.Println("Hello World")
}

I tried to compile above code with go and gccgo then got results below

~/go/src/hello $ go build -o hello_go hello.go
~/go/src/hello $ gccgo -o hello_gccgo hello.go

~/go/src/hello $ ls -lh
-rwxr-xr-x 1 pi pi  17K Dec 10 14:12 hello_gccgo
-rwxr-xr-x 1 pi pi 1.7M Dec 10 14:12 hello_go
-rw-r--r-- 1 pi pi   72 Sep  9 04:20 hello.go

Also you should get below message without target or source.

~/go/src/hello $ gccgo
gccgo: fatal error: no input files
compilation terminated.

Could you try gccgo -o result source.go directly?

I’m sorry I misunderstood your question.
I will rewrite.

Sorry again.

Cgo is a built-in tool from go release so if go installed properly, cgo should also works.

Could you try go tool then check cgo is exist in list?

If not, reinstall go would better.

when am running go tool i got this

go tool: no tool directory: open: /usr/local/go/lib/gccgo/tool: no such file or directory.

I check cgo, cgo is in the directory /usr /local/go/pkg/tool/linux_arm not in the /usr/local/go/lib/gccgo/tool directory.

If you using machine like raspberry pi, /usr /local/go/pkg/tool/linux_arm is right.

As above link, GOTOOLDIR is not an editable env var.

Could you check your env - env and go env?

I also tested at raspberry pi and my env varables are below.
GOPATH & GOBIN & PATH are the vars for go

~ $ env
LS_COLORS=~too~long~
SSH_CONNECTION=~~ip addresses~~
LANG=en_GB.UTF-8
XDG_SESSION_ID=c40
USER=pi
>> GOPATH=/home/pi/go
PWD=/home/pi
HOME=/home/pi
TEXTDOMAIN=Linux-PAM
SSH_CLIENT=~~~~
SSH_TTY=/dev/pts/1
MAIL=/var/mail/pi
TERM=xterm
SHELL=/bin/bash
SHLVL=1
LOGNAME=pi
XDG_RUNTIME_DIR=/run/user/1000
>> (In my memory, I set manually) GOBIN=/home/pi/go/bin
>> PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/local/go/bin:/home/pi/go/bin
_=/usr/bin/env

Here is my go env

~ $ go env
GOARCH="arm"
GOBIN="/home/pi/go/bin"
GOCACHE="/home/pi/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pi/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm"
GCCGO="/usr/bin/gccgo"
GOARM="6"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build485197352=/tmp/go-build -gno-record-gcc-switches"
1 Like

oh sorry.
it turned out that I was wrong to set the GOPATH environment variable
and after changing it, I can go build my go file.

Thank you for the help, the help is very useful :+1::+1::+1:

2 Likes

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