Skip to content

cmd/cgo: -Wunused-variable warning when calling a C function of type: void (*func)(void) #76861

@josephlr

Description

@josephlr

Go version

go version go1.25.4 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/joerichey/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/joerichey/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/5m/z1mvvbh96w1bzx49721xsh_h00cpn8/T/go-build2277445497=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/joerichey/dev/test/tmp/go.mod'
GOMODCACHE='/Users/joerichey/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/joerichey/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/joerichey/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.25.4'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

When using CGO to call C code, if the called function looks like:

void foo(void) {}

a -Wunused-variable warning will be emitted.

Full example:

// #cgo CFLAGS: -Wall
// void foo(void) {}
import "C"

func main() {
	C.foo()
}

Note that if:

  • A function is not called
  • A function takes in any argument
  • A function returns a value

then no warning will be emitted. This example compiles fine:

// #cgo CFLAGS: -Wall
// int bar(void) { return 5; }
// void baz(int x) {}
import "C"

func main() {
	C.bar()
	C.baz(7)
}

What did you see happen?

Running:

go build .

outputs:

# example.com/example
cgo-gcc-prolog:49:33: warning: unused variable '_cgo_a' [-Wunused-variable]

What did you expect to see?

CGO binding could should not create unused variables by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions