go语言如何调用c函数

Python014

go语言如何调用c函数,第1张

直接嵌入c源代码到go代码里面

package main

/*

#include <stdio.h>

void myhello(int i) {

printf("Hello C: %d\n", i)

}

*/

import "C"

import "fmt"

func main() {

C.myhello(C.int(12))

fmt.Println("Hello Go")

}

需要注意的是C代码必须放在注释里面

import "C"语句和前面的C代码之间不能有空行

运行结果

$ go build main.go &&./main

Hello C: 12

Hello Go

分开c代码到单独文件

嵌在一起代码结构不是很好看,很多人包括我,还是喜欢把两个分开,放在不同的文件里面,显得干净,go源文件里面是go的源代码,c源文件里面是c的源代码。

$ ls

hello.c hello.h main.go

$ cat hello.h

void hello(int)

$ cat hello.c

#include <stdio.h>

void hello(int i) {

printf("Hello C: %d\n", i)

}

$ cat main.go

package main

// #include "hello.h"

import "C"

import "fmt"

func main() {

C.hello(C.int(12))

fmt.Println("Hello Go")

}

编译运行

$ go build &&./main

Hello C: 12

Hello Go

编译成库文件

如果c文件比较多,最好还是能够编译成一个独立的库文件,然后go来调用库。

$ find mylib main

mylib

mylib/hello.h

mylib/hello.c

main

main/main.go

编译库文件

$ cd mylib

# gcc -fPIC -shared -o libhello.so hello.c

编译go程序

$ cd main

$ cat main.go

package main

// #cgo CFLAGS: -I../mylib

// #cgo LDFLAGS: -L../mylib -lhello

// #include "hello.h"

import "C"

import "fmt"

func main() {

C.hello(C.int(12))

fmt.Println("Hello Go")

}

$ go build main.go

运行

$ export LD_LIBRARY_PATH=../mylib

$ ./main

Hello C: 12

Hello Go

在我们的例子中,库文件是编译成动态库的,main程序链接的时候也是采用的动态库

$ ldd main

linux-vdso.so.1 => (0x00007fffc7968000)

libhello.so =>../mylib/libhello.so (0x00007f513684c000)

libpthread.so.0 =>/lib64/libpthread.so.0 (0x00007f5136614000)

libc.so.6 =>/lib64/libc.so.6 (0x00007f5136253000)

/lib64/ld-linux-x86-64.so.2 (0x000055d819227000)

理论上讲也是可以编译成整个一静态链接的可执行程序,由于我的机器上缺少静态链接的系统库,比如libc.a,所以只能编译成动态链接。

nd lay around it every day. He climbed to the tree top, ate the apples, took a nap under the shadow... He loved the tree and the tree loved to play with him. 很久很久以前,有一棵又高又大的苹果树.一位小男孩,天天到树下来,他爬上去摘苹果吃,在树荫下睡觉.他爱苹果树,苹果树也爱和他一起玩耍. Time went by... the little boy had grown up and he no longer played around the tree every day. One day, the boy came back to the tree and he looked sad. “Come and play with me,” the tree asked the boy. “I am no longer a kid, I don’t play around trees anymore.” The boy replied, “I want toys. I need money to buy them.”“Sorry, but I don’t have money...but you can pick all my apples and sell them. So, you will have money.” The boy was so excited. He grabbed all the apples on the tree and left happily. The boy never came back after he picked the apples. The tree was sad. 后来,小男孩长大了,不再天天来玩耍.一天他又来到树下,很伤心的样子.苹果树要和他一起玩,男孩说:“不行,我不小了,不能再和你玩,我要玩具,可是没钱买.”苹果树说:“很遗憾,我也没钱,不过,把我所有的果子摘下来卖掉,你不就有钱了?”男孩十分激动,他摘下所有的苹果,高高兴兴地走了.然后,男孩好久都没有来.苹果树很伤心.