如何在CGO中包装一个库(带有c ++ 11特性)

时间:2018-02-01 14:35:45

标签: c++11 go clang cgo openfst

  1. 我的操作系统是Mac,编译器是铿锵
  2. 我想在Go中包装一个C ++库(openfst,它使用C ++ 11特性)
    • 我在How to use C++ in Go?中遵循Scott Wales的方法,并且他给出的演示效果很好(并且不需要编写makefile)
    • 首先为C ++库编写一个C包装器。
    • 使用cgo导入C版本头文件
    • 编写测试文件并运行go test进行测试。
  3. 我遇到了由c ++ 11问题引起的错误。似乎fstlib.h包含一些使用c ++ 11语法的头文件。
  4. In file included from gofst.cpp:2:
    In file included from /usr/local/include/fst/fstlib.h:28:
    In file included from /usr/local/include/fst/expanded-fst.h:13:
    In file included from /usr/local/include/fst/log.h:26:
    /usr/local/include/fst/flags.h:143:50: error: a space is required between consecutive right angle brackets (use '> >')
    /usr/local/include/fst/flags.h:174:37: error: a space is required between consecutive right angle brackets (use '> >')
    
    1. 我尝试使用export CGO_CFLAGS="-g -O2 -std=c++11 -stdlib=libc++"来使用C ++ 11,但我得到了这个。
    2. error: invalid argument '-std=c++11' not allowed with 'C/ObjC'
      FAIL    _/Users/chaoyang/Learn/go/cgo/gofst [build failed]
      

      Belows是所有文件

      gofst.h

      //gofst.h
      
      #ifndef GO_FST_H
      #define GO_FST_H
      
      #ifdef __cplusplus
      extern "C" {
      #endif
        typedef void* Fst;
      
        Fst FstInit(void);
        void FstFree(Fst f);
        void AddState(Fst f);
        void SetStart(Fst f, int startState);
      #ifdef __cplusplus
      }
      #endif
      
      #endif
      

      gofst.cpp

      //gofst.cpp
      #include <fst/fstlib.h>
      #include "gofst.h"
      
      Fst FstInit()
      {
        StdVectorFst fst = new StdVectorFst();
        return (void*)fst;
      }
      void FstFree(Fst f)
      {
        StdVectorFst * fst = (StdVectorFst*)f;
        delete fst;
      }
      
      void AddState(Fst f)
      {
        StdVectorFst * fst = (StdVectorFst*)f;
        f->AddState();
      }
      
      void SetStart(Fst f, int startState)
      {
        StdVectorFst * fst = (StdVectorFst*)f;
        f->SetStart(startState);
      }
      

      gofst.go

      // gofst.go
      package gofst
      
      // #include "gofst.h"
      import "C"
      
      //Fst structy
      type Fst struct {
          fst C.Fst
      }
      
      //New create a new Fst object
      func New() Fst {
          var ret Fst
          ret.fst = C.FstInit()
          return ret
      }
      
      //Free free the fst object
      func (f Fst) Free() {
          C.FstFree(f.fst)
      }
      
      //AddState add a new state for fst
      func (f Fst) AddState() {
          C.AddState(f.fst)
      }
      
      //SetStart set a new id for start state
      func (f Fst) SetStart(state int) {
          C.SetStart(f.fst, state)
      }
      

      gofst_test.go

      // gofst_test.go
      package gofst
      
      import "testing"
      
      func TestFfst(t *testing.T) {
          fst := New()
          fst.AddState()
          fst.SetStart(0)
          foo.Free()
      }
      

      谢谢!

0 个答案:

没有答案