http://tonybai.com/2012/09/26/interoperability-between-go-and-c/

  1. // foo.h
  2.  
  3. int count;
  4. void foo();
  5.  
  6. //foo.c
  7. #include "foo.h"
  8.  
  9. int count = ;
  10. void foo() {
  11. printf("I am foo!\n");
  12. }
  1. //foo.go
  2.  
  3. package main
  4.  
  5. // #cgo LDFLAGS: -L ./ -lfoo
  6. // #include <stdio.h>
  7. // #include <stdlib.h>
  8. // #include "foo.h"
  9. import "C"
  10. import "fmt“
  11.  
  12. func main() {
  13. fmt.Println(C.count)
  14. C.foo()
  15. }
使用静态库
$> gcc -c foo.c
$> ar rv libfoo.a foo.o
  1. [diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.go
  2. [diego@localhost ~/GoWork/src/applycation/testCgo]# ./foo
  3.  
  4. I am foo!
  1. [diego@localhost ~/GoWork/src/applycation/testCgo]# gcc -fPIC -shared -o libfoo.so foo.c
  2. [diego@localhost ~/GoWork/src/applycation/testCgo]# rm li
  3. libfoo.a libfoo.so*
  4. [diego@localhost ~/GoWork/src/applycation/testCgo]# rm libfoo.a
  5. [diego@localhost ~/GoWork/src/applycation/testCgo]# go build fo
  6. foo* foo.c foo.go foo.h foo.o
  7. [diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.
  8. foo.c foo.go foo.h foo.o
  9. [diego@localhost ~/GoWork/src/applycation/testCgo]# go build foo.go
  10. [diego@localhost ~/GoWork/src/applycation/testCgo]# ./foo
  11.  
  12. I am foo!
  13. [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代码:

  1. func TestCallback() {
  2.     f1 := syscall.NewCallback(PlusOne)
  3.     f2 := syscall.NewCallbackCDecl(PlusTwo)
  4.     var m uint32 = 20
  5.     var n uint32 = 80
  6.  
  7.     // Func1 __stdcall
  8.     fmt.Println(C.Func1(C.uint(m), (*[0]byte)(unsafe.Pointer(f1)))) // 21
  9.  
  10.     // Func2 __cdecl
  11.     fmt.Println(C.Func2(C.uint(n), (*[0]byte)(unsafe.Pointer(f2)))) // 82
  12. }
  13.  
  14. func PlusOne(n uint32) uintptr {
  15.     return uintptr(+ 1)
  16. }
  17.  
  18. func PlusTwo(n uint32) uintptr {
  19.     return uintptr(+ 2)
  20. }

C.Func1的第二个参数类型为函数,所以要传入一个*[0]byte。   http://studygolang.com/articles/2629

http://blog.giorgis.io/cgo-examples

Go与C语言的互操作 cgo的更多相关文章

  1. [转]Go与C语言的互操作

    Go有强烈的C背景,除了语法具有继承性外,其设计者以及其设计目标都与C语言有着千丝万缕的联系.在Go与C语言互操作(Interoperability)方面,Go更是提供了强大的支持.尤其是在Go中使用 ...

  2. Go语言练习:go语言与C语言的交互——cgo

    1.代码 package main import "fmt" /* #include <stdlib.h> #include <stdio.h> void ...

  3. go与c语言的互操作

    https://tonybai.com/2012/09/26/interoperability-between-go-and-c/ https://tonybai.com/2016/02/21/som ...

  4. C语言和go语言之间的交互

    一.go代码中使用C代码 go代码中使用C代码,在go语言的函数块中,以注释的方式写入C代码,然后紧跟import "C" 即可在go代码中使用C函数 代码示例: go代码:tes ...

  5. C语言和go语言之间的交互 - C语言中使用go语言,使用的go语言又使用了c语言

    一.go语言中使用C语言 go代码中使用C代码,在go语言的函数块中,以注释的方式写入C代码,然后紧跟import “C” 即可在go代码中使用C函数 代码示例: go代码:testC.go 1 pa ...

  6. [C++/CLI编程宝典][2]什么是C++/CLI语言

    对于什么是C++/CLI,我们首先能够简单的将其名字划分为两部分来理解,第一,C++,我们熟悉的眼下被广泛使用的面向对象的ISO国际标准的高级语言,也称为ISOC++,我们这里以后均称其为ISOC++ ...

  7. C# 语言规范_版本5.0 (第17章 特性)

    1. 特性 C# 语言的一个重要特征是使程序员能够为程序中定义的实体指定声明性信息.例如,类中方法的可访问性是通过使用 method-modifiers(public.protected.intern ...

  8. C# 语言规范_版本5.0 (第0章 目录)

    C# 语言规范 版本5.0 注意 © 1999-2012 Microsoft Corporation.保留所有权利. Microsoft.Windows.Visual Basic.Visual C# ...

  9. C#6.0语言规范(十七) 特性

    许多C#语言使程序员能够指定有关程序中定义的实体的声明性信息.例如,在一个类中的方法的可访问性由与装饰它指定method_modifier小号public,protected,internal,和pr ...

随机推荐

  1. 【05】Number图解

    [05]Number图解  

  2. Java 正则表达式详解---https://www.jb51.net/article/16829.htm

    一.正则表达式基础知识 我们先从简单的开始.假设你要搜索一个包含字符“cat”的字符串,搜索用的正则表达式就是“cat”.如果搜索对大小写不敏感,单词“catalog”.“Catherine”.“so ...

  3. mongodb权限机制以及扩展

    mongodb权限机制 启动权限机制之前要先在MONGODB中添加管理员账号: 1. 创建账号 重装安装一个mongodb,安装时添加一个 --auth参数: 先把安装好的从服务中删除掉(删除之后数据 ...

  4. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

  5. 02-offsetLeft和offsetTop

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. hdu6073[dfs+删边] 2017多校4

    题目中对二分图的定义十分特殊, 指的是 U,V两部分中,U的顶点度数必定为2,V中顶点无限制. 题目要求的是 对于所有匹配,该匹配的权值=该匹配中选中的边的边权的乘积,求所有匹配权值之和. 对于V中的 ...

  7. 使用Unity做2.5D游戏教程(一)

    最近在研究Unity 3D,看了老外Marin Todorov写的教程很详细,就翻译过来以便自己参考,翻译不好的地方请多包涵. 如果你不了解2.5D游戏是什么,它基本上是个3D游戏而你可以想象是压扁的 ...

  8. 【bzoj4408】[Fjoi 2016]神秘数 主席树

    题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1+1+14 = 45 = 4+16 = 4+1+1 ...

  9. Django REST

    一.什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度 ...

  10. 【NOI2012】骑行川藏

    获得成就:第一次在信竞做神仙数学题 先放个前言,$OI$ 出大型数学题还是比较麻烦的,因为主要是考你数学推导 / 手算式子,你算出来之后把公式套个板子,就得到结论——$OI$ 的大型数学题的代码都是板 ...