GoLang之调用C接口的使用方法
来源:互联网 发布:农副产品交易网软件 编辑:程序博客网 时间:2024/05/31 18:32
2014-06-10 wcdj
摘要:本文主要介绍在GoLang中如何实现调用C接口。由于Go的官网经常被墙,导致无法浏览官方的详细文档,偶然间在浏览GoLang的源码中找到了一些关于cgo的用法,具体路径在go/misc/cgo目录下。
例如,在go/misc/cgo/gmp/gmp.go文件中可以找到如何在GoLang中引用C Library的方法:
An example of wrapping a C library in Go. This is the GNU multiprecision library gmp's integer type mpz_t wrapped to look like the Go package big's integer type Int.
下面是一个简单的例子:hello.go
完整的代码可以浏览这里:https://github.com/gerryyang/goinaction/blob/master/src/cgo/src/hello.go
package main/*#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include "../inc/hello.h"// intentionally write the same LDFLAGS differently#cgo linux LDFLAGS: -L../lib -lhello#cgo darwin LDFLAGS: -L../lib -lhello#if 0void hello(const char *name){printf("%s\n", name);return;}#endif*/import "C"import ("fmt""time")func Hello(s string) {cs := C.CString(s)C.hello(cs)}func main() {Seed(1000)fmt.Println(int(C.random()))time.Sleep(time.Duration(1) * time.Second)fmt.Println(int(C.random()))fmt.Println("getpid:", int(C.getpid()))C.puts(C.CString("call C puts"))Hello("call C's go wrapper func")C.hello(C.CString("call C hello func"))}func Seed(i int) {C.srandom(C.uint(i))}
参考
[1] http://www.cnblogs.com/yjf512/archive/2012/07/19/2599304.html
[2] http://tonybai.com/tag/cgo/
0 0
- GoLang之调用C接口的使用方法
- Golang里调用C
- golang中调用C
- Golang tag 的使用方法
- golang之flag简单使用方法
- golang之cgo---调用C/C++动态库函数
- C/C++调用Golang 一
- C/C++调用Golang 二
- c#接口使用方法
- JNI之------C调用java接口
- GoLang之Concurrency协程goroutine使用方法
- c++中调用c接口的问题
- C#winForm调用WebService的远程接口
- [C#]winForm调用WebService的远程接口
- Go语言学习之cgo(golang与C语言相互调用)
- Golang生成共享库(shared library)以及Golang生成C可调用的动态库.so和静态库.a
- Golang与C互用以及调用C的so动态库和a静态库
- SWIG之为C/C++的API生成Python调用接口基础
- 如何在CSDN博客中的所贴的代码进行【代码块】显示
- sass和less,优秀的前端样式预处理器
- 学生聚类分析思考
- mysql编码、数据表编码查看和修改总结
- 【亦观察】 京东能够逆袭阿里凭什么?
- GoLang之调用C接口的使用方法
- 放弃腾讯微信!微软小冰进驻米聊和易信
- cocos2dx去掉黑色背景----ccBlendFunc的使用
- C++ 库
- 黑马程序员_ThreadLocal_20
- atmaslld sdsv
- 第三周__冒泡排序和归并排序
- uva 11462 Age Sort
- 小菜鸟要开始加油 -- 致自己