打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
golang的cgo支持调用C++的方法 | Golang中文社区(Go语言构建) | Go语言中文网 | Go语言学习园地
1)swift,貌似官网的推荐
2)extern "C"
我使用后者,用起来比较爽,上代码
c.h
1 #pragma once2 3 #ifdef __cplusplus4 extern "C" {5 #endif6 void test();7 #ifdef __cplusplus 8 }9 #endif
c.c
1 #include "cplus.hpp"2 #include "c.h"3 4 void test() {5 A *a = new B();6 a->test();7 }
cplus.hpp
1 #pragma once 2 3 class A { 4 public: 5 virtual void test(); 6 }; 7 class B: public A { 8 public: 9 void test();10 };
cplus.cpp
#include <iostream>#include "cplus.hpp"using namespace std;void A::test() { cout << "a" << endl;}void B::test() { cout << "b" << endl;}
build.sh
1 g++ -o cplus.o -c cplus.cpp2 g++ -o c.o -c c.c3 ar r libc_test.so c.o cplus.o
test.go
1 package main 2 // #cgo LDFLAGS: -L . -lc_test -lstdc++ 3 // #cgo CFLAGS: -I ./ 4 // #include "c.h" 5 import "C" 6 7 func main(){ 8 9 C.test()10 11 }
执行顺序
1 ./build.sh2 go build test.go
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在Android中运行go程序(Golang 1.0.2, ARM 6)
golang生成c
golang调用c++文件
手把手教你实现Golang跨平台编译
【Go语言入门100题】021 重要的话说三遍 (5 分) Go语言 | Golang
Go 语言系列40:CGO 编程入门(一)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服