github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/misc/cgo/testsovar/cgoso.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cgosotest
     6  
     7  // This test verifies that Go can access C variables
     8  // in shared object file via cgo.
     9  
    10  /*
    11  // intentionally write the same LDFLAGS differently
    12  // to test correct handling of LDFLAGS.
    13  #cgo windows CFLAGS: -DIMPORT_DLL
    14  #cgo linux LDFLAGS: -L. -lcgosotest
    15  #cgo dragonfly LDFLAGS: -L. -l cgosotest
    16  #cgo freebsd LDFLAGS: -L. -l cgosotest
    17  #cgo openbsd LDFLAGS: -L. -l cgosotest
    18  #cgo solaris LDFLAGS: -L. -lcgosotest
    19  #cgo netbsd LDFLAGS: -L. libcgosotest.so
    20  #cgo darwin LDFLAGS: -L. libcgosotest.dylib
    21  #cgo windows LDFLAGS: -L. libcgosotest.dll
    22  
    23  #include "cgoso_c.h"
    24  
    25  const char* getVar() {
    26  	    return exported_var;
    27  }
    28  */
    29  import "C"
    30  
    31  import "fmt"
    32  
    33  func Test() {
    34  	const want = "Hello world"
    35  	got := C.GoString(C.getVar())
    36  	if got != want {
    37  		panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
    38  	}
    39  	got = C.GoString(C.exported_var)
    40  	if got != want {
    41  		panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
    42  	}
    43  }