Created
December 23, 2011 17:08
-
-
Save surma/1514784 to your computer and use it in GitHub Desktop.
Cgo Testcase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include $(GOROOT)/src/Make.inc | |
TARG=testcase | |
CGOFILES=\ | |
testcase.go\ | |
CGO_OFILES=\ | |
testcase.o\ | |
include $(GOROOT)/src/Make.pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "testcase.h" | |
#include "_cgo_export.h" | |
void CallC(struct MyData *data) { | |
Callback(data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testcase | |
import ( | |
"fmt" | |
// "unsafe" | |
) | |
// #cgo CFLAGS: -I. | |
// #include "testcase.h" | |
import "C" | |
func CallMe() { | |
data := C.struct_MyData {4} | |
C.CallC(&data) | |
} | |
//export Callback | |
func Callback(data *C.struct_MyData) { | |
fmt.Printf("Data: %d\n", data.Number) | |
} | |
// // This however works: | |
// //export Callback | |
// func Callback(data unsafe.Pointer) { | |
// fmt.Printf("Data: %d\n", ((*C.struct_MyData)(data)).Number) | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TESTCASE_H | |
#define TESTCASE_H | |
struct MyData { | |
int Number; | |
}; | |
void CallC(struct MyData *data); | |
#endif // TESTCASE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment