使用C++库时出现错误,包括

huangapple go评论92阅读模式
英文:

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

问题

我正在尝试将一个C++库导入到Go应用程序中。

据说Go可以链接到C++文件...至少Go文档是这么说的(我正在使用Go 1.3)。我不认为它将其识别为C++,但我对C++的了解不多,所以不确定出了什么问题。

它似乎在说它不认识<string>作为C++的包含文件。

给出的编译错误是:

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

我的test.go文件看起来像这样:

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`)
}

有什么办法可以让它工作吗?

英文:

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 &lt;string&gt; 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 &lt;string&gt;
                   ^
compilation terminated.

My test.go file just looks like this:

package main

/*
#include &quot;api-main-binarize.cc&quot;
*/
import &quot;C&quot;

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?

答案1

得分: 2

你无法直接翻译C++代码,你需要使用swig(在1.4版本上有问题)或者编写C包装器来处理你的C++代码。

你可以查看https://stackoverflow.com/a/1721230上的答案,了解更详细的解释。

英文:

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.

huangapple
  • 本文由 发表于 2014年10月3日 12:38:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/26173350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定