久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Go 中使用 C++

How to use C++ in Go(如何在 Go 中使用 C++)
本文介紹了如何在 Go 中使用 C++的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在新的 Go 語言中,我如何調用 C++ 代碼?換句話說,我如何包裝我的 C++ 類并在 Go 中使用它們?

In the new Go language, how do I call C++ code? In other words, how can I wrap my C++ classes and use them in Go?

推薦答案

更新:我已經成功地將一個小型測試 C++ 類與 Go 鏈接起來

Update: I've succeeded in linking a small test C++ class with Go

如果您使用 C 接口包裝 C++ 代碼,您應該能夠使用 cgo 調用您的庫(請參閱 $GOROOT/misc/cgo/gmp 中的 gmp 示例).

If you wrap you C++ code with a C interface you should be able to call your library with cgo (see the example of gmp in $GOROOT/misc/cgo/gmp).

我不確定 C++ 中類的想法是否真的可以在 Go 中表達,因為它沒有繼承.

I'm not sure if the idea of a class in C++ is really expressible in Go, as it doesn't have inheritance.

這是一個例子:

我有一個 C++ 類定義為:

I have a C++ class defined as:

// foo.hpp
class cxxFoo {
public:
  int a;
  cxxFoo(int _a):a(_a){};
  ~cxxFoo(){};
  void Bar();
};

// foo.cpp
#include <iostream>
#include "foo.hpp"
void
cxxFoo::Bar(void){
  std::cout<<this->a<<std::endl;
}

我想在 Go 中使用.我會用C接口

which I want to use in Go. I'll use the C interface

// foo.h
#ifdef __cplusplus
extern "C" {
#endif
  typedef void* Foo;
  Foo FooInit(void);
  void FooFree(Foo);
  void FooBar(Foo);
#ifdef __cplusplus
}
#endif

(我使用 void* 而不是 C 結構,所以編譯器知道 Foo 的大小)

(I use a void* instead of a C struct so the compiler knows the size of Foo)

實現是:

//cfoo.cpp
#include "foo.hpp"
#include "foo.h"
Foo FooInit()
{
  cxxFoo * ret = new cxxFoo(1);
  return (void*)ret;
}
void FooFree(Foo f)
{
  cxxFoo * foo = (cxxFoo*)f;
  delete foo;
}
void FooBar(Foo f)
{
  cxxFoo * foo = (cxxFoo*)f;
  foo->Bar();
}

完成所有這些后,Go 文件是:

with all that done, the Go file is:

// foo.go
package foo
// #include "foo.h"
import "C"
import "unsafe"
type GoFoo struct {
     foo C.Foo;
}
func New()(GoFoo){
     var ret GoFoo;
     ret.foo = C.FooInit();
     return ret;
}
func (f GoFoo)Free(){
     C.FooFree(unsafe.Pointer(f.foo));
}
func (f GoFoo)Bar(){
     C.FooBar(unsafe.Pointer(f.foo));
}

我用來編譯這個的makefile是:

The makefile I used to compile this was:

// makefile
TARG=foo
CGOFILES=foo.go
include $(GOROOT)/src/Make.$(GOARCH)
include $(GOROOT)/src/Make.pkg
foo.o:foo.cpp
    g++ $(_CGO_CFLAGS_$(GOARCH)) -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $<
cfoo.o:cfoo.cpp
    g++ $(_CGO_CFLAGS_$(GOARCH)) -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $<
CGO_LDFLAGS+=-lstdc++
$(elem)_foo.so: foo.cgo4.o foo.o cfoo.o
    gcc $(_CGO_CFLAGS_$(GOARCH)) $(_CGO_LDFLAGS_$(GOOS)) -o $@ $^ $(CGO_LDFLAGS)

嘗試使用以下方法進行測試:

Try testing it with:

// foo_test.go
package foo
import "testing"
func TestFoo(t *testing.T){
    foo := New();
    foo.Bar();
    foo.Free();
}

您需要使用 make install 安裝共享庫,然后運行 ??make test.預期輸出為:

You'll need to install the shared library with make install, then run make test. Expected output is:

gotest
rm -f _test/foo.a _gotest_.6
6g -o _gotest_.6 foo.cgo1.go foo.cgo2.go foo_test.go
rm -f _test/foo.a
gopack grc _test/foo.a _gotest_.6  foo.cgo3.6
1
PASS

這篇關于如何在 Go 中使用 C++的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 欧美日韩在线一区二区 | 欧美区在线 | 国产精品视频在线播放 | 91中文字幕在线观看 | 成人av网站在线观看 | 精品1区 | 天天插天天操 | 欧美黄色片 | 国产在线第一页 | 中文字幕一区二区在线观看 | 国产成人精品亚洲日本在线观看 | 久久久久亚洲 | 99热在线播放 | 亚洲高清在线视频 | 午夜视频一区 | 国产高清视频在线观看 | 国产精品自拍一区 | 欧美性另类 | 在线视频a | 日本午夜精品一区二区三区 | 日韩免费视频 | 久久久www成人免费无遮挡大片 | 国产在线精品一区二区三区 | 亚洲久久久| 国产成都精品91一区二区三 | 国产免费又黄又爽又刺激蜜月al | 国产一级毛片视频 | 日韩视频91 | 给我免费的视频在线观看 | 国产精品美女久久久久久久久久久 | 91资源在线 | 一区二区不卡 | 成人三级电影 | 国产一区二区三区久久久久久久久 | 三级在线观看 | 天天操一操 | 国产一级黄色网 | 综合久久av| 4h影视| 在线视频成人 | 伊人久久成人 |