cgoso.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. package cgosotest
  5. // This test verifies that Go can access C variables
  6. // in shared object file via cgo.
  7. /*
  8. // intentionally write the same LDFLAGS differently
  9. // to test correct handling of LDFLAGS.
  10. #cgo windows CFLAGS: -DIMPORT_DLL
  11. #cgo linux LDFLAGS: -L. -lcgosotest
  12. #cgo dragonfly LDFLAGS: -L. -l cgosotest
  13. #cgo freebsd LDFLAGS: -L. -l cgosotest
  14. #cgo openbsd LDFLAGS: -L. -l cgosotest
  15. #cgo solaris LDFLAGS: -L. -lcgosotest
  16. #cgo netbsd LDFLAGS: -L. libcgosotest.so
  17. #cgo darwin LDFLAGS: -L. libcgosotest.dylib
  18. #cgo windows LDFLAGS: -L. libcgosotest.a
  19. #cgo aix LDFLAGS: -L. -l cgosotest
  20. #include "cgoso_c.h"
  21. const char* getVar() {
  22. return exported_var;
  23. }
  24. */
  25. import "C"
  26. import "fmt"
  27. func Test() {
  28. const want = "Hello world"
  29. got := C.GoString(C.getVar())
  30. if got != want {
  31. panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
  32. }
  33. got = C.GoString(C.exported_var)
  34. if got != want {
  35. panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
  36. }
  37. }