golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.85k stars 17.65k forks source link

cmd/cgo: build failed when any code have CFLAGS: -Werror=unused-vairable #8420

Open gopherbot opened 10 years ago

gopherbot commented 10 years ago

by Snyh1010:

1. What is a short input program that triggers the error?
package main

//#cgo CFLAGS: -Werror=unused-variable
//void test(){}
import "C"

func main() {
        C.test()
}

2. What is the full compiler output?
snyh:shm$go build
# _/run/shm
./a.go: In function ‘_cgo_043ca501ac1f_Cfunc_test’:
./a.go:32:49: error: unused variable ‘a’ [-Werror=unused-variable]
cc1: some warnings being treated as errors

------------------------------------------
I have fixed this by  a CL https://golang.org/cl/118890044/  

The  CL(https://golang.org/cl/109640045)  had fix this problem under gcc. 
The first time I hasn't notice clang, so when the CL has been submitted, it broken
freebsd because clang.  And the CL has been undo.

The broken problem is not caused by my code, it's appeared upon the submission because
the CL contains the test code let the problem appeared. 

I have fixed the problem  both clang and gcc.  But it seems no one have confidence to
submit it this time, I have send the CL request. 

Anyway I want the problem be fixed, with or without my CL.
ianlancetaylor commented 10 years ago

Comment 1:

Labels changed: added repo-main, release-go1.4.

mdempsky commented 10 years ago

Comment 2:

At least since 2.95, GCC has supported the "unused" attribute to suppress warnings about
unused variables.  I've prepared https://golang.org/cl/121550043/ to make use
of this in cgo.
It also looks like -Werror=unused-variable has been supported since GCC 4.2, so it
should be available on OS X and BSD too.  Though even if we wanted to support an
old-timey platform using an older GCC, we could just add an appropriate "// +build
!openbsd,vax" line to issue8420.go.
snyh: Can you please confirm whether the above CL adequately addresses your needs?
rsc commented 10 years ago

Comment 3:

I'm not touching this for the 1.4 release. There is an easy fix. Do not say CFLAGS:
-Werror=unused-variable.

Labels changed: added release-none, removed release-go1.4.

Status changed to Accepted.

ysmolski commented 5 years ago

Compiling this program

package main

// #cgo CFLAGS: -Werror=unused-variable
// void test(){}
import "C"

func main() {
    C.test()
}

on mac gives me:

...go/src/tmp % go build
# tmp
./main.go:8:2: could not determine kind of name for C.test

@mdempsky should we fix this?

ianlancetaylor commented 5 years ago

It's fine if someone wants to fix this, but it's not an important bug to fix.

mohamadpk commented 5 years ago

disable c go on environment variable or CGO_ENABLED=0