Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: CFLAGS not passed in godefs mode #41072

Open
Helflym opened this issue Aug 27, 2020 · 4 comments
Open

cmd/cgo: CFLAGS not passed in godefs mode #41072

Helflym opened this issue Aug 27, 2020 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@Helflym
Copy link
Contributor

Helflym commented Aug 27, 2020

What version of Go are you using (go version)?

Master, 1.15, 1.14.7 and 1.13.13

What operating system and processor architecture are you using (go env)?

Found on aix/ppc64, but same on linux/amd64

What did you do?

The #cgo CFLAGS directive is ignored when using -godefs mode.
The following example shows it with a define. Of course, #cgo CFLAGS: -D_D1 can be replaced by #define _D1 and it will work. But that's not possible, AFAIK, for gcc flags like "-fms-extensions" which I need to pass for another code.

package p

/*
#cgo CFLAGS: -D_D1

#ifdef _D1
typedef struct S1 {
        int a;
} S1_t;
#endif
*/
import "C"

type S1 C.struct_S1

What did you expect to see?

$ go tool cgo -godefs  godefs.go 
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs godefs.go

package p

type S1 struct {
	A int32
}

What did you see instead?

$ go tool cgo -godefs  godefs.go 
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs godefs.go

package p

type S1 struct{}
@Helflym Helflym changed the title cmd/cgo: CGLAFS not taken in godefs mode cmd/cgo: CFLAGS not passed in godefs mode Aug 27, 2020
@ianlancetaylor
Copy link
Contributor

The only way to use -godefs mode is via go tool cgo, and when you run go tool cgo you can pass C compiler options on the command line. Do we need to also support #cgo CFLAGS?

@Helflym
Copy link
Contributor Author

Helflym commented Aug 28, 2020

I wasn't aware you can pass compiler options directly to go tool cgo. Maybe, it should be said in cgo doc, at least that #cgo directives don't work with it.

Anyway, it's up to you, adding #cgo CFLAGS support can be useful in some cases. As you can record which flags to add, it would help if you want to have the same godefs file for different OS without wondering each time which flags are needed. But as there is a way to achieve the same behavior already it's not mandatory, I guess.

@ianlancetaylor
Copy link
Contributor

I suppose I'm OK with having -godefs honor #cgo CFLAGS, but it's not something I plan to work on myself.

@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label Aug 28, 2020
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Aug 28, 2020
@chenk008
Copy link

chenk008 commented Aug 4, 2021

I have the same issue. #cgo directives don't work with go tool cgo. I think adding #cgo CFLAGS support is useful, it can help me to generate c source file.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants