mirror of https://github.com/golang/go.git
misc/cgo/testso: add test for fixed issue 4339
Update #4339. Change-Id: Ic1a7535562b8b824ba166777725f7ba5b9623d77 Reviewed-on: https://go-review.googlesource.com/8523 Run-TryBot: Minux Ma <minux@golang.org> Reviewed-by: Minux Ma <minux@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
f077505d9a
commit
9fa9f966e9
|
|
@ -2,6 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "cgoso_c.h"
|
||||||
#include "_cgo_export.h"
|
#include "_cgo_export.h"
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
@ -12,3 +13,7 @@ void init() {
|
||||||
#else
|
#else
|
||||||
void init() {}
|
void init() {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
const char* getVar() {
|
||||||
|
return exported_var;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
package cgosotest
|
package cgosotest
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
#cgo windows CFLAGS: -DIMPORT_DLL
|
||||||
// intentionally write the same LDFLAGS differently
|
// intentionally write the same LDFLAGS differently
|
||||||
// to test correct handling of LDFLAGS.
|
// to test correct handling of LDFLAGS.
|
||||||
#cgo linux LDFLAGS: -L. -lcgosotest
|
#cgo linux LDFLAGS: -L. -lcgosotest
|
||||||
|
|
@ -15,14 +16,32 @@ package cgosotest
|
||||||
#cgo darwin LDFLAGS: -L. libcgosotest.dylib
|
#cgo darwin LDFLAGS: -L. libcgosotest.dylib
|
||||||
#cgo windows LDFLAGS: -L. libcgosotest.dll
|
#cgo windows LDFLAGS: -L. libcgosotest.dll
|
||||||
|
|
||||||
|
#include "cgoso_c.h"
|
||||||
|
|
||||||
void init(void);
|
void init(void);
|
||||||
void sofunc(void);
|
void sofunc(void);
|
||||||
|
const char* getVar(void);
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func Test() {
|
func Test() {
|
||||||
C.init()
|
C.init()
|
||||||
C.sofunc()
|
C.sofunc()
|
||||||
|
testExportedVar()
|
||||||
|
}
|
||||||
|
|
||||||
|
func testExportedVar() {
|
||||||
|
const want = "Hello world"
|
||||||
|
got := C.GoString(C.getVar())
|
||||||
|
if got != want {
|
||||||
|
panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
|
||||||
|
}
|
||||||
|
got = C.GoString(C.exported_var)
|
||||||
|
if got != want {
|
||||||
|
panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//export goCallback
|
//export goCallback
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
// +build ignore
|
// +build ignore
|
||||||
|
|
||||||
|
#include "cgoso_c.h"
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
// A Windows DLL is unable to call an arbitrary function in
|
// A Windows DLL is unable to call an arbitrary function in
|
||||||
// the main executable. Work around that by making the main
|
// the main executable. Work around that by making the main
|
||||||
|
|
@ -28,3 +30,5 @@ void sofunc(void)
|
||||||
{
|
{
|
||||||
goCallback();
|
goCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *exported_var = "Hello world";
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#if defined(EXPORT_DLL)
|
||||||
|
# define VAR __declspec(dllexport)
|
||||||
|
#elif defined(IMPORT_DLL)
|
||||||
|
# define VAR __declspec(dllimport)
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
# define VAR
|
||||||
|
#endif
|
||||||
|
|
||||||
|
VAR const char *exported_var;
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
@echo off
|
@echo off
|
||||||
|
|
||||||
gcc -c cgoso_c.c
|
gcc -c cgoso_c.c -DEXPORT_DLL
|
||||||
gcc -shared -o libcgosotest.dll cgoso_c.o
|
gcc -shared -o libcgosotest.dll cgoso_c.o
|
||||||
if not exist libcgosotest.dll goto fail
|
if not exist libcgosotest.dll goto fail
|
||||||
go build main.go
|
go build main.go
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue