0

I'm attempting to import a C++ library into a Go app.

Supposedly Go can link to C++ files... or at least that's what the Go Doc says (I'm using Go 1.3.) I don't think it's recognizing it as C++, but I don't really understand much of C++ so I'm not sure what's going on.

It appears to be saying that it doesn't recognized <string> as a C++ include.

The compile error it gives me is:

# go build test.go
# command-line-arguments
In file included from api-main-binarize.cc:14:0,
                 from ./test.go:4:
doc-binarize.h:15:19: fatal error: string: No such file or directory
 #include <string>
                   ^
compilation terminated.

My test.go file just looks like this:

package main

/*
#include "api-main-binarize.cc"
*/
import "C"

func Threshold(infile, outfile string) {
    C.threshold(C.char(infile), C.char(outfile))
}

func main() {
    Threshold(`test.jp2`, `test.pbm`)
}

Any ideas how to make this work?

Alasdair
  • 13,348
  • 18
  • 82
  • 138

1 Answers1

2

You can't, you will have to either use swig (broken on 1.4) or write C wrappers for your C++ code.

Check https://stackoverflow.com/a/1721230 answer for a longer explanation.

Community
  • 1
  • 1
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • @Alasdair Yes, you can link against c++ objects, but you can't include c++ headers directly, you will have to create a c wrapper for it. – OneOfOne Oct 03 '14 at 04:48
  • How heavy is the overhead if I'm calling C from Go and C++ from C? Is it still running C++ compiled code at C++ speed? – Alasdair Oct 03 '14 at 04:53
  • @Alasdair Depends on what you're doing. If your c++ function takes several seconds to do work, the overhead is extremely unnoticeable but if it returns right away then the overhead will be more noticeable – OneOfOne Oct 03 '14 at 04:55