- My OS is Mac and the compiler is clang
- I want to wrap a C++ library (openfst , which uses C++11 feature ) in Go
- I follow the method of Scott Wales in How to use C++ in Go? , and the demo given by him works well( and no need to write makefile)
- First write a C wrapper for the C++ library.
- Use cgo to import the C version header file
- Write a test file and run go testto test.
 
- I got errors caused by c++11 issues. It seems the fstlib.h contains some header files which use c++11 syntax.
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 '> >')
- I try to use export CGO_CFLAGS="-g -O2 -std=c++11 -stdlib=libc++"to use C++11 but I got this.
error: invalid argument '-std=c++11' not allowed with 'C/ObjC'
FAIL    _/Users/chaoyang/Learn/go/cgo/gofst [build failed]
Belows are all the files
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()
}
Thanks!
