ℙℕℤℝ 2009-11-11 05:25
浏览 405
已采纳

如何在围棋中使用 c + + ?

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?

转载于:https://stackoverflow.com/questions/1713214/how-to-use-c-in-go

  • 写回答

11条回答 默认 最新

  • 撒拉嘿哟木头 2009-11-12 10:12
    关注

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

    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).

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

    Here's an example:

    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;
    }
    

    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
    

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

    The implementation is:

    //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();
    }
    

    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));
    }
    

    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();
    }
    

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示