问题描述
自从进入1.5出来的时候,我开始服用再看看我如何把它们融入我的现有项目。
Ever since Go 1.5 came out, I started taking another look at how I could integrate it into an existing project of mine.
该项目的codeBase的是用C写的完全是低级别的访问硬件和其他有趣的东西。然而,一些更高层次的东西是单调乏味的,我想开始在一个更高层次的语言写他们(围棋)
The project's codebase is written entirely in C for low level access to to hardware and other fun stuff. However, some of the higher level things are tedious, and I would like to start writing them in a higher level language (Go)
有什么办法,我可以从C程序中调用转到code?我安装了围棋1.5,它增加了 -buildmode = C-归档
()。
Is there any way I can call Go code from a C program? I installed Go 1.5, which added -buildmode=c-archive
(https://golang.org/s/execmodes) which I am trying to get working.
不过,我似乎无法得到转到生成相应的头文件,让我的项目实际编译。当我产生归档,我看到导出的符号功能(使用objdump的),但没有头文件,包括GCC抱怨的功能不存在(如预期)
However, I can't seem to get Go to generate the appropriate header files to allow my project to actually compile. When I generate the archive, I see the function in the exported symbols (using objdump), but without the header files to include gcc complains about the function not existing (as expected)
我是很新的去 - 但是,我爱的语言,并想利用它。有没有得到这个与对方发挥很好的任何习惯的方法(习惯被使用了很多围棋的世界里,我看到了)?
I'm quite new to Go - however, I love the language and would like to make use of it. Is there any idiomatic way ("idiomatic" gets used a lot in the world of Go I see...) to get this to play nicely with each other?
我问这个问题,并特别提到了围棋1.5是,根据该文件,<一个原因href=\"https://docs.google.com/document/d/1nr-TQHw_er6GOQRsF6T43GGhFDelrAP0NqSS_00RgZQ/edit?pli=1#heading=h.1gw5ytjfcoke\">https://docs.google.com/document/d/1nr-TQHw_er6GOQRsF6T43GGhFDelrAP0NqSS_00RgZQ/edit?pli=1#heading=h.1gw5ytjfcoke
去1.5补充支持非围棋程序调用转到code。具体地讲,在一节中提到去code挂入,并从非围棋程序名为
The reason I asked this question and specifically mentioned Go 1.5 is that according to this document, https://docs.google.com/document/d/1nr-TQHw_er6GOQRsF6T43GGhFDelrAP0NqSS_00RgZQ/edit?pli=1#heading=h.1gw5ytjfcokeGo 1.5 added support for non-Go programs to call Go code. Specifically, mentioned under the section "Go code linked into, and called from, a non-Go program"
推荐答案
要建立由C来调用存档,您需要将它们标记为出口CGO的符号。结果
例如,如果我创建一个文件 foo.go
包含以下内容:
To build an archive callable from C, you will need to mark them as exported CGo symbols.
For example, if I create a file foo.go
with the following contents:
package main
import (
"C"
"fmt"
)
//export PrintInt
func PrintInt(x int) {
fmt.Println(x)
}
func main() {}
要注意的重要事情是:
- 包需要被命名为
主
- 您需要有一个
主
功能,虽然它可以是空的。 - 您需要导入的包
C
- 您需要特殊的
//出口
注释来标记你想从C调用的功能。
- The package needs to be called
main
- You need to have a
main
function, although it can be empty. - You need to import the package
C
- You need special
//export
comments to mark the functions you want callable from C.
我可以编译为使用以下命令一个C调用静态库:
I can compile it as a C callable static library with the following command:
go build -buildmode=c-archive foo.go
结果将是一个存档 foo.a
和标题 foo.h中
。在标题中,我们得到以下(eliding无关的部分):
The results will be an archive foo.a
and a header foo.h
. In the header, we get the following (eliding irrelevant parts):
...
typedef long long GoInt64;
...
typedef GoInt64 GoInt;
...
extern void PrintInt(GoInt p0);
...
所以,这足以叫导出的函数。我们可以编写调用它像这样一个简单的C程序:
So that's enough to call the exported function. We can write a simple C program that calls it like so:
#include "foo.h"
int main(int argc, char **argv) {
PrintInt(42);
return 0;
}
我们可以用命令编译如下:
We can compile it with a command like:
gcc -pthread foo.c foo.a -o foo
是必须的 -pthread
选项,因为Go运行时利用线程。当我运行生成的可执行文件它打印 42
。
The -pthread
option is needed because the Go runtime makes use of threads. When I run the resulting executable it prints 42
.
这篇关于在现有的C项目使用转到code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!