local-host 2009-11-11 05:25 采纳率: 100%
浏览 387
已采纳

如何在围棋中使用 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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug