Alasdair
Alasdair

Reputation: 14113

Go: Using C++ Library: Error including <string>

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?

Upvotes: 0

Views: 3095

Answers (1)

OneOfOne
OneOfOne

Reputation: 99274

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.

Upvotes: 3

Related Questions