如何在不使用make的情况下使用cgo?

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

How do you use cgo WITHOUT using make?

问题

我使用一个自定义构建工具来编译Go项目,我需要一种方法来在我的项目中使用cgo。

问题是cgo文档只告诉你如何在make中使用它。

我真正需要知道的是要使用哪些生成的文件以及使用哪些工具以及需要按什么顺序进行处理。我尝试阅读go源目录中的make.pkg,但我的最佳努力失败了。

我的测试dll非常简单,只有一个函数,每次调用时返回1,使用这个函数的Go代码也同样简单。

在成功运行cgo项目的make命令后,控制台输出的结果将非常有帮助。

英文:

I use a custom build tool to compile go projects and I need a way to use cgo in my project.

The problem is that the cgo documentation only tells you how to use it with make.

What I really need to know is which generated files to process with which tools and in what order it needs to be done. I tried to read make.pkg in the go source dir but my best effort fails.

My test dll is very simple, a single function that returns 1 every time it is called and the go code to use this function is similarly simple.

The output from the console produced by a successful run of make on a cgo project would be very helpful.

答案1

得分: 3

在32位Linux上运行<code>make</code>命令在misc/cgo/life目录中的输出结果:

# gomake _obj/life.a
CGOPKGPATH= cgo --  life.go 
touch _obj/_cgo_run
8g  -o _go_.8  _obj/life.cgo1.go _obj/_cgo_gotypes.go
8c -FVw -I ${GOROOT}/pkg/linux_386 -I . -o &quot;_cgo_defun.8&quot; _obj/_cgo_defun.c
gcc -m32 -I . -g -fPIC -O2 -o _cgo_main.o -c   _obj/_cgo_main.c
gcc -m32 -g -fPIC -O2 -o c-life.o -c   c-life.c
gcc -m32 -I . -g -fPIC -O2 -o life.cgo2.o -c   _obj/life.cgo2.c
gcc -m32 -I . -g -fPIC -O2 -o _cgo_export.o -c   _obj/_cgo_export.c
gcc -m32 -g -fPIC -O2 -o _cgo1_.o _cgo_main.o c-life.o life.cgo2.o _cgo_export.o  
cgo -dynimport _cgo1_.o &gt;_obj/_cgo_import.c_ &amp;&amp; mv -f _obj/_cgo_import.c_ _obj/_cgo_import.c
8c -FVw -I . -o &quot;_cgo_import.8&quot; _obj/_cgo_import.c
rm -f _obj/life.a
gopack grc _obj/life.a _go_.8  _cgo_defun.8 _cgo_import.8 c-life.o  life.cgo2.o _cgo_export.o

<code>cgo -- life.go</code>这一行创建了以下文件:

_obj/_cgo_.o
_obj/life.cgo1.go
_obj/life.cgo2.c
_obj/_cgo_gotypes.go
_obj/_cgo_defun.c
_obj/_cgo_main.c
_obj/_cgo_flags
_obj/_cgo_export.c
_cgo_export.h
英文:

Output of running <code>make</code> on 32-bit Linux in directory misc/cgo/life:

# gomake _obj/life.a
CGOPKGPATH= cgo --  life.go 
touch _obj/_cgo_run
8g  -o _go_.8  _obj/life.cgo1.go _obj/_cgo_gotypes.go
8c -FVw -I ${GOROOT}/pkg/linux_386 -I . -o &quot;_cgo_defun.8&quot; _obj/_cgo_defun.c
gcc -m32 -I . -g -fPIC -O2 -o _cgo_main.o -c   _obj/_cgo_main.c
gcc -m32 -g -fPIC -O2 -o c-life.o -c   c-life.c
gcc -m32 -I . -g -fPIC -O2 -o life.cgo2.o -c   _obj/life.cgo2.c
gcc -m32 -I . -g -fPIC -O2 -o _cgo_export.o -c   _obj/_cgo_export.c
gcc -m32 -g -fPIC -O2 -o _cgo1_.o _cgo_main.o c-life.o life.cgo2.o _cgo_export.o  
cgo -dynimport _cgo1_.o &gt;_obj/_cgo_import.c_ &amp;&amp; mv -f _obj/_cgo_import.c_ _obj/_cgo_import.c
8c -FVw -I . -o &quot;_cgo_import.8&quot; _obj/_cgo_import.c
rm -f _obj/life.a
gopack grc _obj/life.a _go_.8  _cgo_defun.8 _cgo_import.8 c-life.o  life.cgo2.o _cgo_export.o

The line <code>cgo -- life.go</code> creates the following files:

_obj/_cgo_.o
_obj/life.cgo1.go
_obj/life.cgo2.c
_obj/_cgo_gotypes.go
_obj/_cgo_defun.c
_obj/_cgo_main.c
_obj/_cgo_flags
_obj/_cgo_export.c
_cgo_export.h

答案2

得分: 1

我使用一个自定义构建工具来编译Go项目,我需要一种在我的项目中使用cgo的方法。

...而这种方法会导致问题。使用标准的Makefile方法简单、易用、经过验证、有文档等。

我意识到我没有(直接)回答你的问题。相反,我的“答案”是:我强烈建议使用标准的方法。不要选择其他不直接支持的选项,以免给自己带来问题。

话虽如此,我认为有一种方法可以避免使用Makefile,只是我从来没有尝试过,抱歉。通常我比较懒/时间不够,所以我会选择最简单/最快的方法来完成任务。你可能也想尝试一下同样的方法;-)

英文:

"I use a custom build tool to compile go projects and I need a way to use cgo in my project."

... and this approach leads to problems. Using the standard way with a Makefile is simple, easy, proven, documented, etc.

I realize I'm not (directly) answering your question. Instead my "answer" is: I strongly suggest to use the standard way. Don't create problems for your self by choosing other, not directly supported options.

That said, I think there is a way to avoid the Makefiles, I just never been there, sorry. I'm usually lazy/short of time, so I use the simplest/fastest way to get things done. You might want to try the same 如何在不使用make的情况下使用cgo?

huangapple
  • 本文由 发表于 2011年12月13日 05:05:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/8480842.html
匿名

发表评论

匿名网友

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

确定