英文:
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 <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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论