栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在现有C项目上使用Go

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在现有C项目上使用Go

这可能不是您想要的,但是在 Go 1.5 中(即将在今年8月推出),您将能够使用go工具构建与C兼容的库。所以有了这个

_main.c

#include <stdio.h>int main(){    char *string_to_pass = NULL;    if (asprintf(&string_to_pass, "This is a test.") < 0) {        printf("asprintf fail");        return -1;    }    PrintString(string_to_pass);    return 0;}

这在你的

main.go

package mainimport "C"import "fmt"//export PrintStringfunc PrintString(cs *C.char) {    s := C.GoString(cs)    fmt.Println(s)}func main() {}

您可以针对静态库执行以下操作:

go build -buildmode c-archive -o mygopkg.agcc -o main _main.c mygopkg.a -lpthread

对于共享库:

go build -buildmode c-shared -o mygopkg.soLD_RUN_PATH=$(pwd) gcc -o main _main.c mygopkg.so -lpthread

LD_RUN_PATH
在这里使链接器在您正在构建的同一目录中查找共享库。)

有关更多信息,请参见Go执行模式设计文档。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573524.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号