3
  1. 我的操作系统是 Mac,编译器是铿锵声
  2. 我想在 Go 中包装一个 C++ 库(openfst,它使用 C++11 功能)
  3. 我遇到了由 c++11 问题引起的错误。似乎 fstlib.h 包含一些使用 c++11 语法的头文件。
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,但我得到了这个。
error: invalid argument '-std=c++11' not allowed with 'C/ObjC'
FAIL    _/Users/chaoyang/Learn/go/cgo/gofst [build failed]

以下是所有文件

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

gost.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()
}

谢谢!

4

0 回答 0