在新的Go语言中,我如何调用c++代码?换句话说,我如何包装我的c++类并在Go中使用它们?
当前回答
从我在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.
其他回答
这里的问题是,兼容的实现不需要将类放在compile .cpp文件中。如果编译器可以优化一个类的存在,只要程序在没有它的情况下也能以同样的方式运行,那么它就可以从输出可执行文件中删除。
C语言有一个标准的二进制接口。因此,您将能够知道您的函数被导出了。但是c++背后并没有这样的标准。
你正走在一个未知的领域。这里是调用C代码的Go示例,也许在阅读了c++命名混乱和调用约定以及大量的试验和错误之后,您可以做一些类似的事情。
如果你还想尝试,祝你好运。
从go1.2+开始,cgo自动合并和编译c++代码:
http://golang.org/doc/go1.2#cgo_and_cpp
从我在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++来识别对标准库的需求。
推荐文章
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- 如何修剪字符串的前导和尾随空白?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- gcc在哪里查找C和c++头文件?