Skip to content

Commit 6a347be

Browse files
committed
cmd/cgo: use -fno-lto in cflags when compiling _cgo_export.c
Currently, cgo declares different types for exported symbols between _cgo_main.c and _cgo_export.c. This is not compatible with link time optimization in GCC and causes builds to fail when compiling with CGO_CFLAGS="-flto -ffat-lto-objects". This commit appends -fno-lto to the cflags when building _cgo_export.c to disable link time optimization locally on the built object. Fixes golang#43830
1 parent ff0e93e commit 6a347be

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2776,9 +2776,14 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
27762776

27772777
// gcc
27782778
cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS)
2779+
exportCflags := str.StringList(cflags, "-fno-lto")
27792780
for _, cfile := range cfiles {
27802781
ofile := nextOfile()
2781-
if err := b.gcc(a, p, a.Objdir, ofile, cflags, objdir+cfile); err != nil {
2782+
currentCflags := cflags
2783+
if cfile == "_cgo_export.c" {
2784+
currentCflags = exportCflags
2785+
}
2786+
if err := b.gcc(a, p, a.Objdir, ofile, currentCflags, objdir+cfile); err != nil {
27822787
return nil, nil, err
27832788
}
27842789
outObj = append(outObj, ofile)

0 commit comments

Comments
 (0)