Building and linking dynamically from a go binary

前端 未结 5 1865
情书的邮戳
情书的邮戳 2020-12-13 02:51

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
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 03:16

    Update: It is now possible to 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 goandroid 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. An mmaped region of shared memory.
      1. That is, create a file on /dev/shm and have both programs mmap it.
      2. The 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.

提交回复
热议问题