Created
October 4, 2017 07:28
-
-
Save kikuchy/7b656aa46662c783fe2ce539fd9a67b1 to your computer and use it in GitHub Desktop.
GoからCの関数をコールバック関数として呼び出したい。けどGoからCの関数ポインタを関数として呼び出すことはできない。のでなんとかした。
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 <stdio.h> | |
#include "worker.h" | |
void cb(int hoge) { | |
printf("value: %d\n", hoge); | |
} | |
int main() { | |
DoSomeCallback(cb); | |
} |
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 main | |
//typedef void (*callback)(int); | |
// | |
//static inline void invokeCallback(int value, callback f) { | |
// f(value); | |
//} | |
import "C" | |
import "fmt" | |
//export DoSomeCallback | |
func DoSomeCallback(callback C.callback) { | |
fmt.Println("in go world") | |
C.invokeCallback(30, callback) | |
} | |
func main() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考にしたところ
https://stackoverflow.com/a/37641451
http://blog.ralch.com/tutorial/golang-sharing-libraries/
https://godoc.org/github.com/gophersjp/go/src/cmd/cgo