从二进制文件动态构建和链接 [英] Building and linking dynamically from a go binary

查看:210
本文介绍了从二进制文件动态构建和链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:


  1. 我在机器上安装了二进制文件

  2. 从这个二进制文件我需要编译一个外部.go文件

  3. 编译完成后,我需要将编译的go文件链接到当前的二进制文件中,以便我可以使用刚刚编译的go代码。 / li>

你认为这可能吗?

我做了一些研究,似乎没有可能,但我可能忽略了一些东西。



感谢:)

第一次去二进制文件将包含类似于

  func main(){
//这里我需要编译一个外部的go文件或包)包含
// runFoo()的定义

//一旦文件/包被编译和链接,我需要调用编译后的代码
runFoo()

//通常在这里继续执行过程


$ div class =h2_lin >解决方案

更新:现在可以在mainline Go中执行此操作,请参阅执行模式



Go 1.5发行说明
$ b


仅针对amd64架构,编译器提供了一个新选项
-dynlink,它可协助动态通过支持对外部共享库中定义的Go符号的引用进行链接。

旧答案讨论其他选项):

目前无法在主线Go中创建动态链接库*。有一些关于这个的讨论,所以你可能会在未来看到支持。但是,有一个名为 goandriod 的第三方推送项目需要您需要的相同功能,因此他们需要维护补丁程序应该允许您修补官方Go代码库以支持您请求的动态链接支持。



如果您想要使用标准的Go运行时,我会推荐以下其中一项。
从您的其他程序中调用您的Go程序,并使用以下方式进行通信:


  1. 管道沟通

  2. UNIX域套接字

  3. 共享内存的哑形区域。


    1. 这是在/ dev / shm上创建一个文件,并同时将这两个程序映射到它。

    2. https://github.com/edsrzf/mmap-go
    3. $ b $每一个连续的选项都需要更多的努力来设置,更具体的平台,但可能比上一个。



      *注意:这是Windows世界中的DLL,UNIX / Linux世界中的.so文件。 >

      My problem is the following:

      1. I have a go binary on a machine
      2. From that binary I need to compile an external .go file
      3. Once compiled, I need to link the compiled go file into the current binary so I can use the just-compiled go code.

      Do you think that's possible ?

      I did a few researches and it does not seem to be possible, but I might have overlooked something.

      Thanks :)

      The first go binary would contain something like

      func main() {
          // Here I need to compile an external go file (or package) which contains
          // The definition of runFoo()
      
          // Once the file/package is compiled and linked I need to call the compiled code
          runFoo()
      
          // Continue the execution process normally here
      }
      

      解决方案

      Update: It is now possible do do this in mainline Go, see Go Execution Modes

      From the Go 1.5 release notes:

      For the amd64 architecture only, the compiler has a new option, -dynlink, that assists dynamic linking by supporting references to Go symbols defined in external shared libraries.

      Old Answer (useful discussion of other options):

      It is not currently possible to create dynamically linked libraries* in main line Go. There has been some talk about this, so you may see support in the future. However, there is a 3rd party go project called goandriod that needed the same functionality you need, so they maintain patches that should allow you to patch the official Go code base to support the dynamic linked support you are requesting.

      If you want to use a the standard Go run-time, I would recommend the one of the following. Invoke your Go program from your other program, and communicate using:

      1. Pipes to communicate
      2. A UNIX domain socket
      3. A mmaped region of shared memory.

        1. That is create a file on /dev/shm and have both programs mmap it.
        2. Go mmap library: https://github.com/edsrzf/mmap-go

      Each consecutive option will take more effort to setup, be more platform specific, but potentially be more powerful than the previous one.

      *Note: That is DLLs in the Windows world, and .so files in the UNIX/Linux world.

      这篇关于从二进制文件动态构建和链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆