在新的Go语言中,我如何调用c++代码?换句话说,我如何包装我的c++类并在Go中使用它们?


当前回答

这里的问题是,兼容的实现不需要将类放在compile .cpp文件中。如果编译器可以优化一个类的存在,只要程序在没有它的情况下也能以同样的方式运行,那么它就可以从输出可执行文件中删除。

C语言有一个标准的二进制接口。因此,您将能够知道您的函数被导出了。但是c++背后并没有这样的标准。

其他回答

从我在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.

在使用gcc Go编译器gccgo时,我们讨论了C和Go之间的互操作性。然而,当使用gccgo时,Go的互操作性和实现的特性集都有限制(例如,有限的goroutines,没有垃圾收集)。

我根据Scott Wales的回答创建了下面的例子。我已经在macOS High Sierra 10.13.3运行go版本go1.10 darwin/amd64上测试了它。

(1) library.hpp的代码,我们要调用的c++ API。

#pragma once
class Foo {
 public:
  Foo(int value);
  ~Foo();
  int value() const;    
 private:
  int m_value;
};

(2) library.cpp的代码,c++实现。

#include "library.hpp"
#include <iostream>

Foo::Foo(int value) : m_value(value) {
  std::cout << "[c++] Foo::Foo(" << m_value << ")" << std::endl;
}

Foo::~Foo() { std::cout << "[c++] Foo::~Foo(" << m_value << ")" << std::endl; }

int Foo::value() const {
  std::cout << "[c++] Foo::value() is " << m_value << std::endl;
  return m_value;
}

(3) library-bridge.h的代码,该桥需要公开c++实现的C API,以便go可以使用它。

#pragma once
#ifdef __cplusplus
extern "C" {
#endif

void* LIB_NewFoo(int value);
void LIB_DestroyFoo(void* foo);
int LIB_FooValue(void* foo);

#ifdef __cplusplus
}  // extern "C"
#endif

(4)代码为library-bridge.cpp,实现桥接。

#include <iostream>

#include "library-bridge.h"
#include "library.hpp"

void* LIB_NewFoo(int value) {
  std::cout << "[c++ bridge] LIB_NewFoo(" << value << ")" << std::endl;
  auto foo = new Foo(value);
  std::cout << "[c++ bridge] LIB_NewFoo(" << value << ") will return pointer "
            << foo << std::endl;
  return foo;
}

// Utility function local to the bridge's implementation
Foo* AsFoo(void* foo) { return reinterpret_cast<Foo*>(foo); }

void LIB_DestroyFoo(void* foo) {
  std::cout << "[c++ bridge] LIB_DestroyFoo(" << foo << ")" << std::endl;
  AsFoo(foo)->~Foo();
}

int LIB_FooValue(void* foo) {
  std::cout << "[c++ bridge] LIB_FooValue(" << foo << ")" << std::endl;
  return AsFoo(foo)->value();
}

(5)最后,图书馆。go程序调用c++ API。

package main

// #cgo LDFLAGS: -L. -llibrary
// #include "library-bridge.h"
import "C"
import "unsafe"
import "fmt"

type Foo struct {
    ptr unsafe.Pointer
}

func NewFoo(value int) Foo {
    var foo Foo
    foo.ptr = C.LIB_NewFoo(C.int(value))
    return foo
}

func (foo Foo) Free() {
    C.LIB_DestroyFoo(foo.ptr)
}

func (foo Foo) value() int {
    return int(C.LIB_FooValue(foo.ptr))
}

func main() {
    foo := NewFoo(42)
    defer foo.Free() // The Go analog to C++'s RAII
    fmt.Println("[go]", foo.value())
}

使用下面的Makefile

liblibrary.so: library.cpp library-bridge.cpp
    clang++ -o liblibrary.so library.cpp library-bridge.cpp \
    -std=c++17 -O3 -Wall -Wextra -fPIC -shared

我可以运行示例程序如下所示:

$ make
clang++ -o liblibrary.so library.cpp library-bridge.cpp \
    -std=c++17 -O3 -Wall -Wextra -fPIC -shared
$ go run library.go
[c++ bridge] LIB_NewFoo(42)
[c++] Foo::Foo(42)
[c++ bridge] LIB_NewFoo(42) will return pointer 0x42002e0
[c++ bridge] LIB_FooValue(0x42002e0)
[c++] Foo::value() is 42
[go] 42
[c++ bridge] LIB_DestroyFoo(0x42002e0)
[c++] Foo::~Foo(42)

重要的

上面在go程序中导入“C”的注释是不可选的。你必须完全按照图中显示的那样放置它们,这样cgo才能知道要加载哪个头文件和库,在这种情况下:

// #cgo LDFLAGS: -L. -llibrary
// #include "library-bridge.h"
import "C"

链接到GitHub repo的完整示例。

看来这是关于戈朗的一个早期问题。同时回答永不更新。在这三到四年间,有太多的新图书馆和博客文章问世了。下面是我觉得有用的几个链接。

SWIG和Go

使用SWIG调用Go中的c++代码

在比较语言方面,c++和Go

GoForCPPProgrammers

你正走在一个未知的领域。这里是调用C代码的Go示例,也许在阅读了c++命名混乱和调用约定以及大量的试验和错误之后,您可以做一些类似的事情。

如果你还想尝试,祝你好运。