C调用GO的动态链接库

来源:互联网 发布:淘宝知网查重靠谱吗 编辑:程序博客网 时间:2024/06/09 17:39

1) GO源代码

[root@test129 test]# cat hello.go

package main

 

import "C"

 

//export Hello

func Hello() string {

       return "Hello"

}

 

func main() {

}

}

2) 编译GO动态链接库

gobuild -v -x -buildmode=c-shared -o libhello.so

会生成libhello.h、libhello.so两个文件。

3) C源代码

[root@test129 test]# cat test.c

#include<stdio.h>

#include "libhello.h"

void main()

{

       printf("%s in C\n", Hello());

}

4) 编译C可执行程序

gcc test.c -lhello -o test



0 0