...

Text file src/cmd/go/testdata/script/cgo_undef.txt

Documentation: cmd/go/testdata/script

     1# Issue 52863.
     2
     3# We manually create a .syso and a .a file in package a,
     4# such that the .syso file only works when linked against the .a file.
     5# Package a has #cgo LDFLAGS to make this happen.
     6#
     7# Package c imports package a, and uses cgo itself.
     8# The generation of the _cgo_import.go for package c will fail,
     9# because it won't know that it has to link against a/libb.a
    10# (because we don't gather the #cgo LDFLAGS from all transitively
    11# imported packages).
    12#
    13# The _cgo_import.go file is only needed for internal linking.
    14# When generating _cgo_import.go for package c fails, an ordinary
    15# external link should still work. But an internal link is expected
    16# to fail, because the failure to create _cgo_import.go should cause
    17# the linker to report an inability to internally link.
    18
    19[short] skip
    20[!cgo] skip
    21[!exec:ar] skip
    22
    23cc -c -o a/b.syso b/b.c
    24cc -c -o b/lib.o b/lib.c
    25exec ar rc a/libb.a b/lib.o
    26
    27go build
    28! stderr 'undefined reference'
    29
    30! go build -ldflags=-linkmode=internal
    31stderr 'some packages could not be built to support internal linking.*m/c|requires external linking|does not support internal cgo'
    32
    33# Test for issue #68743.
    34go build -x m/d
    35! stderr 'undefined reference'
    36stderr 'test for internal linking'
    37
    38-- go.mod --
    39module m
    40
    41-- a/a.go --
    42package a
    43
    44// #cgo LDFLAGS: -L. -lb
    45// extern int CFn(int);
    46import "C"
    47
    48func GoFn(v int) int { return int(C.CFn(C.int(v))) }
    49
    50-- b/b.c --
    51extern int LibFn(int);
    52int CFn(int i) { return LibFn(i); }
    53
    54-- b/lib.c --
    55int LibFn(int i) { return i; }
    56
    57-- c/c.go --
    58package c
    59
    60// static int D(int i) { return i; }
    61import "C"
    62
    63import "m/a"
    64
    65func Fn(i int) (int, int) {
    66     return a.GoFn(i), int(C.D(C.int(i)))
    67}
    68
    69-- d/d.go --
    70// Package d is a copy of package c, to build with -x.
    71package d
    72
    73// static int D(int i) { return i; }
    74import "C"
    75
    76import "m/a"
    77
    78func Fn(i int) (int, int) {
    79     return a.GoFn(i), int(C.D(C.int(i)))
    80}
    81
    82-- main.go --
    83package main
    84
    85import "m/c"
    86
    87func main() {
    88	println(c.Fn(0))
    89}

View as plain text