C语言调用GO语言生成的C静态库

一开始看别人的例子,本身弄老是不成功,后来也是去GO语言社区看多几个例子,找找规律,才把几个本身没注意到的细(keng)节填起来了。ubuntu

GO语言写一个函数app

cktest.go函数

package main

import (
        "C"
        "fmt"
)

//export test1
func test1(str string) {
        fmt.Println("THis is my test" + str)
}

func main() {
}

这里有几个须要注意的地方ui

1. 必须有 //export test1 ,对函数的注释,不然不能生成 .h 文件。spa

Go functions can be executed from C applications. They should be exported by using the following comment line: //export <your_function_name>code

2. 必须有import C, 不然不能生成静态库string

3. package main, 必须是main,  若是不是, 生成的静态库没法使用,编译连接时会出现cktest.a: error adding symbols: Archive has no index; run ranlib to add one的错误io

4. func main() 必需要main函数,不然不能生成静态库编译

 

go build -o cktest.a -buildmode=c-archive cktest.go社区

buildmode中c-archive是静态库,c-shared是动态库

生成静态库和头文件

root@ubuntu:/mnt/d/workspace/src/cktest# go build -o cktest.a -buildmode=c-archive cktest.go
root@ubuntu:/mnt/d/workspace/src/cktest# ls
cktest.a  cktest.go  cktest.h
// cktest.h

...
typedef struct { const char *p; GoInt n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif


extern void test1(GoString p0);

#ifdef __cplusplus
}
#endif

C语言写一个主函数

main.c

#include <stdio.h>
#include <string.h>
#include "cktest.h"


int main ()
{
        GoString str;
        char a[] = "hello   hahaha . . . ";
        str.p = a;
        str.n = strlen(a);
        test1(str);
        return 0;
}

这里要include头文件,test1()函数声明在头文件里看,str参数就会变成GoString.

编译连接静态库,生成可执行文件

gcc -o main main.c cktest.a -lpthread

root@ubuntu:/mnt/d/workspace/src/cktest# gcc -o main main.c cktest.a -lpthread
root@ubuntu:/mnt/d/workspace/src/cktest# 
root@ubuntu:/mnt/d/workspace/src/cktest# ls
cktest.a  cktest.go  cktest.h  main  main.c

执行,成功调用了GO语言里的函数。

root@ubuntu:/mnt/d/workspace/src/cktest# ./main
THis is my testhello   hahaha . . .