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

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. luogu2604 [ZJOI2010]网络扩容

    先做一遍普通的dinic 然后再更改源点为超级源,超级源向原源加一条capacity=k && cost=0的边,再加上有费用的边跑最小费用最大流 #include <iostr ...

  2. PHP读取xlsx Excel 文件

    <?php require_once 'simplexlsx.class.php'; if ( $xlsx = SimpleXLSX::parse('pricelist.xlsx') ) { p ...

  3. 【转】Python + Android + Uiautomator自动化测试

    1.首先来介绍下UIAutomator工具 UIAutomator是Android官方推出的安卓应用界面自动化测试工具,是最理想的针对APK进行自动化功能回归测试的利器. 2.UIAutomator测 ...

  4. jsp 详解request对象

    request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1  object ...

  5. Spring-IOC源码解读2.3-BeanDefinition的注册

    在DefaultListAbleBeanFactory中通过一个HashMap持有载入的BeanDefinition信息 ,这个HashMap的定义在DefaultListAbleBeanFactor ...

  6. Subsequence(hdu 3530)

    题意:给你一个长度为n的数列,要求一个子区间,使得区间的最大值与最小值的差s满足,m<=s<=k,求满足条件的最长子区间 /* 单调队列 我们可以用单调队列分别维护最大值和最小值 当差值大 ...

  7. Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

    D - MUH and Cube Walls Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  8. 标准C程序设计七---16

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  9. msp430项目编程55

    msp430综合项目---扩展项目五55 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  10. MySQL实现了四种通信协议

    原文链接:http://blog.csdn.net/yangling132/article/details/50932705[侵删] TCP/IP协议,通常我们通过来连接MySQL,各种主要编程语言都 ...