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  [short] skip
    20  [!cgo] skip
    21  [!exec:ar] skip
    22  
    23  cc -c -o a/b.syso b/b.c
    24  cc -c -o b/lib.o b/lib.c
    25  exec ar rc a/libb.a b/lib.o
    26  go build
    27  ! go build -ldflags=-linkmode=internal
    28  stderr 'some packages could not be built to support internal linking.*m/c|requires external linking|does not support internal cgo'
    29  
    30  -- go.mod --
    31  module m
    32  
    33  -- a/a.go --
    34  package a
    35  
    36  // #cgo LDFLAGS: -L. -lb
    37  // extern int CFn(int);
    38  import "C"
    39  
    40  func GoFn(v int) int { return int(C.CFn(C.int(v))) }
    41  
    42  -- b/b.c --
    43  extern int LibFn(int);
    44  int CFn(int i) { return LibFn(i); }
    45  
    46  -- b/lib.c --
    47  int LibFn(int i) { return i; }
    48  
    49  -- c/c.go --
    50  package c
    51  
    52  // static int D(int i) { return i; }
    53  import "C"
    54  
    55  import "m/a"
    56  
    57  func Fn(i int) (int, int) {
    58       return a.GoFn(i), int(C.D(C.int(i)))
    59  }
    60  
    61  -- main.go --
    62  package main
    63  
    64  import "m/c"
    65  
    66  func main() {
    67  	println(c.Fn(0))
    68  }
    69  

View as plain text