How to use a dynamically linked library from relative path

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

How to use a dynamically linked library from relative path

问题

我想在Go应用程序中使用一个动态的C库,我可以构建应用程序,但是在运行时找不到库文件。
这是我的项目结构:

src/ctest/
      |- lib/
      |    |- libmylib.so
      |    |- libmylib.h
      |- main.go

在main.go中,我导入了.h和.so文件:

/*
#cgo CFLAGS: -I./lib
#cgo LDFLAGS: -L./lib -lmylib
#include <mylib.h>
*/
import "C"

func main() {
    C.testMyLib()
}

我可以成功构建应用程序,但是当启动时,它会抛出以下错误:

error while loading shared libraries: libmylib.so.0: cannot open shared object file: No such file or directory

如果我将libmylib.so文件复制到/usr/lib中,一切都按预期工作;然而,我希望我的应用程序在运行时自动在CURRENT_PATH/lib中搜索所需的库,而不需要设置环境变量。我该如何实现这一点?

英文:

I would like to use a dynamic C library from a go application, I can build the application but the library is not found at runtime.
Here the structure of my project:

src/ctest/
      |- lib/
      |    |- libmylib.so
      |    |- libmylib.h
      |- main.go

in main.go I import the .h and .so files:

/*
#cgo CFLAGS: -I./lib
#cgo LDFLAGS: -L./lib -lmylib
#include &lt;mylib.h&gt;
*/
import &quot;C&quot;

func main() {
    C.testMyLib()
}

I can build the application successfully, but when launched it throws this error:

error while loading shared libraries: libmylib.so.0: cannot open shared object file: No such file or directory

If I copy the libmylib.so file in /usr/lib then everything works as expected; however, I would like that my application automatically searches for the needed library in CURRENT_PATH/lib at runtime without setting environment variables. How can I achieve it?

答案1

得分: 5

我能够解决这个问题,只需在 main.go 文件的 LDFLAGS 选项中添加 -Wl,-rpath=\$ORIGIN/lib 链接器标志:

package main
/*
#cgo CFLAGS: -I${SRCDIR}/lib
#cgo LDFLAGS: -L${SRCDIR}/lib -Wl,-rpath=$ORIGIN/lib -luiohook
#include <uiohook.h>
*/
import "C"

func main() {
    C.hook_run()
}

现在,当应用程序执行时,它还会使用 CURRENT_FOLDER/lib 来搜索动态库(CURRENT_FOLDER 是应用程序可执行文件所在的目录)。

仅适用于 Linux 用户:
如果仍然出现错误,您需要创建一个符号链接或将 XXX.so 库重命名为 XXX.so.0。在我的情况下,如下所示:

src/ctest/
  |- lib/
  |    |- libmylib.so
  |    |- libmylib.so.0 <- 符号链接指向 ./libmylib.so
  |    |- libmylib.h
  |- main.go
英文:

I was able to solve the issue adding the -Wl,-rpath=\$ORIGIN/lib linker flag to LDFLAGS options in the main.go file:

package main
/*
#cgo CFLAGS: -I${SRCDIR}/lib
#cgo LDFLAGS: -L${SRCDIR}/lib -Wl,-rpath=$ORIGIN/lib -luiohook
#include &lt;uiohook.h&gt;
*/
import &quot;C&quot;

func main() {
    C.hook_run()
}

Now when the application is executed, it uses also the CURRENT_FOLDER/lib to search for dynamic libraries (CURRENT_FOLDER is the directory where the application executable is executed).

For linux users only:
If the error is still thrown, you need to create a symlink or to rename the XXX.so library to XXX.so.0. In my case it was:

src/ctest/
  |- lib/
  |    |- libmylib.so
  |    |- libmylib.so.0 &lt;- symlink to ./libmylib.so
  |    |- libmylib.h
  |- main.go

huangapple
  • 本文由 发表于 2017年5月27日 06:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/44210731.html
匿名

发表评论

匿名网友

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

确定