Go与C语言的互操作 cgo
2024-08-27 04:15:33
http://tonybai.com/2012/09/26/interoperability-between-go-and-c/
// foo.h int count;
void foo(); //foo.c
#include "foo.h" int count = ;
void foo() {
printf("I am foo!\n");
}
//foo.go package main // #cgo LDFLAGS: -L ./ -lfoo
// #include <stdio.h>
// #include <stdlib.h>
// #include "foo.h"
import "C"
import "fmt“ func main() {
fmt.Println(C.count)
C.foo()
}
使用静态库
$> gcc -c foo.c
$> ar rv libfoo.a foo.o
[diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.go
[diego@localhost ~/GoWork/src/applycation/testCgo]# ./foo I am foo!
[diego@localhost ~/GoWork/src/applycation/testCgo]# gcc -fPIC -shared -o libfoo.so foo.c
[diego@localhost ~/GoWork/src/applycation/testCgo]# rm li
libfoo.a libfoo.so*
[diego@localhost ~/GoWork/src/applycation/testCgo]# rm libfoo.a
[diego@localhost ~/GoWork/src/applycation/testCgo]# go build fo
foo* foo.c foo.go foo.h foo.o
[diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.
foo.c foo.go foo.h foo.o
[diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.go
[diego@localhost ~/GoWork/src/applycation/testCgo]# ./foo I am foo!
[diego@localhost ~/GoWork/src/applycation/testCgo]#
http://tonybai.com/2012/09/26/interoperability-between-go-and-c/
与在Go中使用C源码相比,在C中使用Go函数的场合较少。在Go中,可以使用"export + 函数名"来导出Go函数为C所使用,看一个简单例子:
package main
/*
#include <stdio.h>
extern void GoExportedFunc();
void bar() {
printf("I am bar!\n");
GoExportedFunc();
}
*/
import "C"
import "fmt"
//export GoExportedFunc
func GoExportedFunc() {
fmt.Println("I am a GoExportedFunc!")
}
func main() {
C.bar()
}
不过当我们编译该Go文件时,我们得到了如下错误信息:
# command-line-arguments
/tmp/go-build163255970/command-line-arguments/_obj/bar.cgo2.o: In function `bar':
./bar.go:7: multiple definition of `bar'
/tmp/go-build163255970/command-line-arguments/_obj/_cgo_export.o:/home/tonybai/test/go/bar.go:7: first defined here
collect2: ld returned 1 exit status
代码似乎没有任何问题,但就是无法通过编译,总是提示“多重定义”。翻看Cgo的文档,找到了些端倪。原来
There is a limitation: if your program uses any //export directives, then the C code in the comment may only include declarations (extern int f();), not definitions (int f() { return 1; }).
似乎是// extern int f()与//export f不能放在一个Go源文件中。我们把bar.go拆分成bar1.go和bar2.go两个文件:
// bar1.go
package main
/*
#include <stdio.h>
extern void GoExportedFunc();
void bar() {
printf("I am bar!\n");
GoExportedFunc();
}
*/
import "C"
func main() {
C.bar()
}
// bar2.go
package main
import "C"
import "fmt"
//export GoExportedFunc
func GoExportedFunc() {
fmt.Println("I am a GoExportedFunc!")
}
编译执行:
$> go build -o bar bar1.go bar2.go
$> bar
I am bar!
I am a GoExportedFunc!
Go代码:
func TestCallback() {
f1 := syscall.NewCallback(PlusOne)
f2 := syscall.NewCallbackCDecl(PlusTwo)
var m uint32 = 20
var n uint32 = 80 // Func1 __stdcall
fmt.Println(C.Func1(C.uint(m), (*[0]byte)(unsafe.Pointer(f1)))) // 21 // Func2 __cdecl
fmt.Println(C.Func2(C.uint(n), (*[0]byte)(unsafe.Pointer(f2)))) // 82
} func PlusOne(n uint32) uintptr {
return uintptr(n + 1)
} func PlusTwo(n uint32) uintptr {
return uintptr(n + 2)
}
C.Func1的第二个参数类型为函数,所以要传入一个*[0]byte。 http://studygolang.com/articles/2629
http://blog.giorgis.io/cgo-examples
最新文章
- Atitit 深入理解软件的本质 attilax总结 软件三原则";三次原则";是DRY原则和YAGNI原则的折
- HoloLens模拟器仿真器与文档现已向开发者们开放
- 通过cookie实现搜索框内容保存关闭浏览器之前的操作、jq js实现方法
- 突破短板,传统桌面程序 使用webapi 扩展迎合web和移动端融合的需求
- autoit UIA获取Listview的信息
- linux考试基础知识测验
- 算法分析-堆排序 HeapSort 优先级队列
- C#之关于时间的整理
- FTP 安装配置
- IIR数字滤波器的实现(C语言)
- 13.14.15.16.17&;《一个程序猿的生命周期》读后感
- uoj 300 [CTSC2017]吉夫特 - Lucas - 分块 - 动态规划
- MQTT 3.1.1,值得升级的6个新特性
- CSS 实例之翻转图片
- webpack学习文档
- 在文章详情页调用seo标题标签
- 联合主键用hibernate注解映射方式主要有三种:
- A: Absolutely Simple---巴什博弈
- nginx(一)- 初识
- C/C++获取本机名+本机IP+本机MAC
热门文章
- Python模块概念
- [转]automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性)
- 安装altium designer的破解问题
- redux学习总结
- ubuntu添加开机启动
- java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment问题解决
- POJ 2643 Election
- bzoj3743 [Coci2015]Kamp 常州模拟赛d6t2
- request.getContextPath是为了解决相对路径的问题,可返回站点的根路径
- System.out.println()和System.out.write()的区别