在新的Go语言中,我如何调用c++代码?换句话说,我如何包装我的c++类并在Go中使用它们?
当前回答
在使用gcc Go编译器gccgo时,我们讨论了C和Go之间的互操作性。然而,当使用gccgo时,Go的互操作性和实现的特性集都有限制(例如,有限的goroutines,没有垃圾收集)。
其他回答
有趣的是,这一声明提到了许多更广泛的问题。Dan Lyke在他的网站Flutterby上进行了一场非常有趣和深思熟虑的讨论,讨论了开发进程间标准作为引导新语言的一种方式(以及其他分支,但这是与本文相关的)。
从我在FAQ中读到的内容来看,你还不能确定:
Do Go programs link with C/C++ programs? There are two Go compiler implementations, gc (the 6g program and friends) and gccgo. Gc uses a different calling convention and linker and can therefore only be linked with C programs using the same convention. There is such a C compiler but no C++ compiler. Gccgo is a GCC front-end that can, with care, be linked with GCC-compiled C or C++ programs. The cgo program provides the mechanism for a “foreign function interface” to allow safe calling of C libraries from Go code. SWIG extends this capability to C++ libraries.
您可能需要向Golang/CGo的LDFlags添加-lc++来识别对标准库的需求。
这可以使用命令cgo来实现。
在本质上 如果导入“C”之前紧接着有一个注释,这个注释被称为preamble,在编译包的C部分时被用作头文件。例如:“ 来源:https://golang.org/cmd/cgo/
// #include <stdio.h>
// #include <errno.h>
import "C"
从go1.2+开始,cgo自动合并和编译c++代码:
http://golang.org/doc/go1.2#cgo_and_cpp
推荐文章
- Std::auto_ptr改为Std::unique_ptr
- int的最大值
- c++中有最大数组长度限制吗?
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- 如何修剪字符串的前导和尾随空白?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何为Fedora安装g++ ?