dongyong6428 2011-12-12 21:05
浏览 70
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dongtui6347 2011-12-13 12:33
    关注

    Output of running make 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 "_cgo_defun.8" _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 >_obj/_cgo_import.c_ && mv -f _obj/_cgo_import.c_ _obj/_cgo_import.c
    8c -FVw -I . -o "_cgo_import.8" _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 cgo -- life.go 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解