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 lin