在现有的C项目使用转到 [英] Using Go on existing C project
问题描述
我有一个程序完全写在使用多个对象(O)
文件进去℃。这些文件都收拾好了一个压缩文件里(一)
,反过来,在程序的主(的C编译时使用)
文件。
I have a program entirely written in C that uses multiple object (.o)
files in it. 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.
我要为这个项目转到一个新的文件。我的想法是写这个。去
文件,然后创建一个对象(O)从它
文件。后来,我想把已经提到存档内这个目标文件(一)
文件。
I want to write a new file for this project in Go. My idea is to write this .go
file and then create an object (.o)
file from it. Afterwards, I want to put this object file inside the already mentioned archive (.a)
file.
这基本上意味着,我想从C程序拨打转到功能。我读过这个问题,而它给我看,我想是可能的通过GCCGO,这不是100%清楚如何做到这一点。
This basically means that I want to call Go functions from a C program. 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.
即使最基本的测试,我在链接阶段出现错误。更具体地讲,这里是一些基本的例子之一:
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
package main
import
(
"fmt"
)
func PrintString(buff string) int {
fmt.Printf(buff)
return 1
}
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;
}
编译
为了编译转到文件,我用这个命令:
In order to compile the Go file, I used this command:
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
这意味着GCCGO的预期在转到主函数文件,而不是C之一。
Which means that GCCGO's expecting a main function in the Go file instead of the C one.
使用 - 静电libgo
, -static
和轮候册, - 在第二个命令R,/路径/要/ libgo.so's_folder
选项产生不同的结果:
Using the --static-libgo
, -static
and -Wl,-R,/path/to/libgo.so's_folder
options on the second command yield a different result:
/usr/bin/ld: cannot find -lgo
collect2: error: ld returned 1 exit status
这是没有意义的,因为我有LD_LIBRARY_PATH环境变量正确指向libgo.so的文件夹中。
Which makes no sense, since I have the LD_LIBRARY_PATH environment variable properly pointing to libgo.so's folder.
我意识到我可能做错了这里,但我看不出那是什么。有旁边没有GCCGO的例子及其用C那里互动,唯一的参考我能找到的是此页,我个人觉得这是不够的。
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.
请问慈祥对此事的一些建议,并感谢您的时间。 :)
I ask kindly for some advice on this matter and thank you for your time. :)
推荐答案
这可能不是你想要的,但在转到1.5,这是未来这个月,你就可以编译C兼容的库,与去工具。所以用这个在你的 _main.c
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. So with this in your _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;
}
这在 main.go
package main
import "C"
import "fmt"
//export PrintString
func PrintString(cs *C.char) {
s := C.GoString(cs)
fmt.Println(s)
}
func main() {}
您可以做的,静态库:
go build -buildmode c-archive -o mygopkg.a
gcc -o main _main.c mygopkg.a -lpthread
有关共享库:
go build -buildmode c-shared -o mygopkg.so
LD_RUN_PATH=$(pwd) gcc -o main _main.c mygopkg.so -lpthread
( LD_RUN_PATH
在这里,使连接器的外观为您构建同一个目录的共享库。)
(LD_RUN_PATH
is here to make the linker look for the shared library in the same directory you're building.)
查看去执行模式设计文档获取更多信息。
这篇关于在现有的C项目使用转到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!