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

     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  [!cgo] skip
    20  [!exec:ar] skip
    21  
    22  cc -c -o a/b.syso b/b.c
    23  cc -c -o b/lib.o b/lib.c
    24  exec ar rc a/libb.a b/lib.o
    25  
    26  go build
    27  ! stderr 'undefined reference'
    28  
    29  ! go build -ldflags=-linkmode=internal
    30  stderr 'some packages could not be built to support internal linking.*m/c|requires external linking|does not support internal cgo'
    31  
    32  # Test for issue #68743.
    33  go build -x m/d
    34  ! stderr 'undefined reference'
    35  stderr 'test for internal linking'
    36  
    37  -- go.mod --
    38  module m
    39  
    40  go 1.18
    41  
    42  -- a/a.go --
    43  package a
    44  
    45  // #cgo LDFLAGS: -L. -lb
    46  // extern int CFn(int);
    47  import "C"
    48  
    49  func GoFn(v int) int { return int(C.CFn(C.int(v))) }
    50  
    51  -- b/b.c --
    52  extern int LibFn(int);
    53  int CFn(int i) { return LibFn(i); }
    54  
    55  -- b/lib.c --
    56  int LibFn(int i) { return i; }
    57  
    58  -- c/c.go --
    59  package c
    60  
    61  // static int D(int i) { return i; }
    62  import "C"
    63  
    64  import "m/a"
    65  
    66  func Fn(i int) (int, int) {
    67       return a.GoFn(i), int(C.D(C.int(i)))
    68  }
    69  
    70  -- d/d.go --
    71  // Package d is a copy of package c, to build with -x.
    72  package d
    73  
    74  // static int D(int i) { return i; }
    75  import "C"
    76  
    77  import "m/a"
    78  
    79  func Fn(i int) (int, int) {
    80       return a.GoFn(i), int(C.D(C.int(i)))
    81  }
    82  
    83  -- main.go --
    84  package main
    85  
    86  import "m/c"
    87  
    88  func main() {
    89  	println(c.Fn(0))
    90  }
    91  

View as plain text