gpt4 book ai didi

go - 从 go 二进制文件动态构建和链接

转载 作者:IT老高 更新时间:2023-10-28 13:05:01 30 4
gpt4 key购买 nike

我的问题如下:

  1. 我的机器上有一个 go 二进制文件
  2. 我需要从该二进制文件编译一个外部 .go 文件
  3. 编译后,我需要将编译后的 go 文件链接到当前的二进制文件中,这样我才能使用刚刚编译的 go 代码。

你认为这可能吗?

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

谢谢:)

第一个 go 二进制文件将包含类似

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
}

最佳答案

Go 1.5 将于 2015 年 8 月推出创建共享库的功能¹。

来自“The State of Go” Andrew Gerrand 的演讲:

Shared libraries

Go 1.5 can produce Go shared libraries that can be consumed by Go programs.

Build the standard library as shared libraries:

$ go install -buildmode=shared std

Build a "Hello, world" program that links against the shared libraries:

$ go build -linkshared hello.go
$ ls -l hello
-rwxr-xr-x 1 adg adg 13926 May 26 02:13 hello

Go 1.5 can also build Go programs as C archive files (for static linking) or shared libraries (for dynamic linking) that can be consumed by C programs.

[See:] golang.org/s/execmodes

¹ 请注意,gccgo 已经对此提供有限支持一段时间了,Go 1.5 将是常规 go 构建工具首次支持此功能。

关于go - 从 go 二进制文件动态构建和链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19431296/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com