How can I specify a relative dylib path in cgo on macOS

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

How can I specify a relative dylib path in cgo on macOS

问题

我目前正在尝试在Go中使用Cgo使一个已存在的用C编写的dylib文件正常工作。
为此,我使用了CFLAGS和LDFLAGS,但没有成功:

// #cgo CFLAGS: -I${SRCDIR}
// #cgo LDFLAGS: -L. -lMyLibrary

我还阅读到可以使用魔术字符串@executable_path来指定相对库搜索路径,但是将其添加到-L选项或尝试将-install_name添加到链接器标志中都没有结果。

这是我得到的链接器错误信息:

/Users/benedikt/sdk/go1.19.7/bin/go build -o /private/var/folders/.../T/GoLand/___1go_build_myapp myapp #gosetup
/private/var/folders/.../T/GoLand/___1go_build_myapp
dyld[58935]: Library not loaded: '/usr/local/lib/libMyLibrary.dylib'
  Referenced from: '/private/var/folders/.../T/GoLand/___1go_build_myapp'
  Reason: tried: '/usr/local/lib/libMyLibrary.dylib' (no such file), '/usr/lib/libMyLibrary.dylib' (no such file)

在不添加任何go之外的构建步骤的情况下,我该如何正确配置Cgo以使用与二进制文件相同文件夹中的dylib,并使其能够通过go build运行?

英文:

I am currently attempting to get an existing dylib written in C working in Go.
For this, I am using Cgo and everything compiles correctly.
When go build attempts to run the resulting executable dyld cannot find the dylib that is located in the Go source directory.

I am using these CFLAGS and LDFLAGS without success:

// #cgo CFLAGS: -I${SRCDIR}
// #cgo LDFLAGS: -L. -lMyLibrary

I also read that I could use the magic string @executable_path in order to specify a relative library search path, but adding it to -L or trying to add -install_name to the linker flags yields no result.

This is the linker error message I get:

/Users/benedikt/sdk/go1.19.7/bin/go build -o /private/var/folders/.../T/GoLand/___1go_build_myapp myapp #gosetup
/private/var/folders/.../T/GoLand/___1go_build_myapp
dyld[58935]: Library not loaded: '/usr/local/lib/libMyLibrary.dylib'
  Referenced from: '/private/var/folders/.../T/GoLand/___1go_build_myapp'
  Reason: tried: '/usr/local/lib/libMyLibrary.dylib' (no such file), '/usr/lib/libMyLibrary.dylib' (no such file)

While not adding any build steps outside go, how can I configure Cgo correctly to use my dylib that is located in the same folder that the binary will be located in later and so that it will run with go build?

答案1

得分: 2

原来我离目标已经很接近了,但实际库中存储的搜索路径是错误的。

在修改库后,我提供的代码可以正常工作:

install_name_tool -id @loader_path/libMyLibrary.dylib libMyLibrary.dylib

虽然我还没有找到一种干净的方法来让 GoLand 控制 go build 将我的库复制到输出目录中,但我初步通过将输出目录设置为绝对路径并添加一个预执行命令来将依赖项复制到输出文件夹中,已经使其工作。

英文:

It turns out I was already pretty close to my goal, but the search path stored inside the actual library was wrong.

The code in my question works fine after modifying the library as follows:

install_name_tool -id @loader_path/libMyLibrary.dylib libMyLibrary.dylib

While I have not yet found a clean way to get go build controlled by GoLand to copy my library to the output directory it runs the application from, I preliminarily got it to work by setting the output directory to an absolute path and adding a pre-execution command that copies the dependencies to the output folder.

huangapple
  • 本文由 发表于 2023年3月8日 04:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75666660.html
匿名

发表评论

匿名网友

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

确定