[英]Using Go on existing C project
I have a program entirely written in C that uses multiple object (.o)
files in it. 我有一个完全用C语言编写的程序,它使用多个对象
(.o)
文件。 These files are all packed inside an archive file (.a)
which, in turn, is used at compile-time of the program's main (.c)
file. 这些文件都打包在一个存档文件
(.a)
中,而后者又在程序主(.c)
文件的编译时使用。
I want to write a new file for this project in Go. 我想在Go中为这个项目写一个新文件。 My idea is to write this
.go
file and then create an object (.o)
file from it. 我的想法是编写这个
.go
文件,然后从中创建一个对象(.o)
文件。 Afterwards, I want to put this object file inside the already mentioned archive (.a)
file. 之后,我想将此对象文件放在已经提到的存档
(.a)
文件中。
This basically means that I want to call Go functions from a C program . 这基本上意味着我想从C程序调用Go函数 。 I've read this question , and while it showed me that what I want is possible via GCCGO, it's not 100% clear as to how to do it.
我已经读过这个问题了 ,虽然它向我展示了我想要的东西可能是通过GCCGO,但它并不是100%清楚如何做到这一点。
Even with the most basic of tests, I get errors during the linking phase. 即使使用最基本的测试,我也会在链接阶段遇到错误。 More specifically, here's one of such basic example:
更具体地说,这是一个基本的例子:
printString.go printString.go
package main
import
(
"fmt"
)
func PrintString(buff string) int {
fmt.Printf(buff)
return 1
}
c_caller.c c_caller.c
#define _GNU_SOURCE
#include <stdio.h>
extern int PrintString(char*) __asm__ ("print.main.PrintString");
int main() {
char *string_to_pass= NULL;
asprintf(&string_to_pass, "This is a test.");
int result= PrintString(string_to_pass);
if(result) {printf("Everything went as expected!\n");}
else {printf("Uh oh, something went wrong!\n");}
return result;
}
Compiling 编译
In order to compile the Go file, I used this command: 为了编译Go文件,我使用了以下命令:
gccgo -c printString.go -o printString.o -fgo-prefix=print -Wall -Werror -march=native
In order to compile the entire thing, I used this command: 为了编译整个东西,我使用了这个命令:
gccgo -o main c_caller.c printString.o -Wall -Werror -march=native
The return message I'm getting is: 我得到的回复信息是:
/usr/lib64/libgo.so.4.0.0: undefined reference to `main.main'
/usr/lib64/libgo.so.4.0.0: undefined reference to `__go_init_main'
collect2: error: ld returned 1 exit status
Which means that GCCGO's expecting a main function in the Go file instead of the C one. 这意味着GCCGO期望Go文件中的主要功能而不是C文件。
Using the --static-libgo
, -static
and -Wl,-R,/path/to/libgo.so's_folder
options on the second command yield a different result: 在第二个命令上使用
--static-libgo
, -static
和--static-libgo
, -Wl,-R,/path/to/libgo.so's_folder
选项会产生不同的结果:
/usr/bin/ld: cannot find -lgo
collect2: error: ld returned 1 exit status
Which makes no sense, since I have the LD_LIBRARY_PATH environment variable properly pointing to libgo.so's folder. 这没有任何意义,因为我有LD_LIBRARY_PATH环境变量正确指向libgo.so的文件夹。
I realize that I'm probably doing something wrong here, but I just can't see what that is. 我意识到我可能在这里做错了什么,但我看不出那是什么。 There's next to no examples of GCCGO and its interaction with C out there, and the only reference I could find was this page , which I personally feel like it's not enough.
接下来没有GCCGO的例子以及它与C的交互,我唯一能找到的参考是这个页面 ,我个人认为这还不够。
I ask kindly for some advice on this matter and thank you for your time. 我请你就这件事提出一些建议,谢谢你的时间。 :)
:)
This may not be what you want, but in Go 1.5, that's coming this August, you'll be able to build C-compatible libraries with the go tool. 这可能不是你想要的,但是在今年8月即将发布的Go 1.5中 ,你将能够使用go工具构建与C兼容的库。 So with this in your
_main.c
所以在你的
_main.c
#include <stdio.h>
int main()
{
char *string_to_pass = NULL;
if (asprintf(&string_to_pass, "This is a test.") < 0) {
printf("asprintf fail");
return -1;
}
PrintString(string_to_pass);
return 0;
}
and this in your main.go
这在你的
main.go
package main
import "C"
import "fmt"
//export PrintString
func PrintString(cs *C.char) {
s := C.GoString(cs)
fmt.Println(s)
}
func main() {}
You can do, for static library: 你可以为静态库做:
go build -buildmode c-archive -o mygopkg.a
gcc -o main _main.c mygopkg.a -lpthread
For shared library: 对于共享库:
go build -buildmode c-shared -o mygopkg.so
LD_RUN_PATH=$(pwd) gcc -o main _main.c mygopkg.so -lpthread
( LD_RUN_PATH
is here to make the linker look for the shared library in the same directory you're building.) (
LD_RUN_PATH
用于使链接器在您正在构建的同一目录中查找共享库。)
See the Go execution modes design document for more info. 有关详细信息,请参阅Go执行模式设计文档 。
There currently isn't a supported way to do what you want. 目前没有支持的方法来做你想要的。 Go always needs the support of its runtime, and the entry point for that is always
main
. Go始终需要其运行时的支持,并且其入口点始终是
main
。 AFAIK, gccgo also makes these same assumptions, and doesn't provide a way to easily link into from other programs. AFAIK,gccgo也做出了同样的假设,并没有提供一种轻松链接到其他程序的方法。
If you want to do this in a supported manner, you will have to wait until go1.5+ where work is being done to compile shared libraries from Go code. 如果您想以受支持的方式执行此操作,则必须等到go1.5 +正在完成工作以从Go代码编译共享库。
If you really want to hack on this now, you can look into how the Android port works using the default gc toolchain with -linkmode external
, which renames main
in the object file and calls it externally. 如果您现在真的想破解这一点,您可以使用带有
-linkmode external
的默认gc工具链来查看Android端口的工作原理,它会在目标文件中重命名main
并在外部调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.