cgo在linux平台调用so库,一直报错

用go的cgo在linux平台调用so库。因为这个so有关联性,所以一直调用不成功。


package main

import (
    "fftest/dylib"
    "fmt"
    "os"
    "runtime"
    "sync"
)

// #cgo CFLAGS: -I./number
// #cgo LDFLAGS: -L${SRCDIR} -lnumber
var avformatDll *dylib.LazyDLL
var avformatDllOnce sync.Once

func main() {
    libPath, _ := os.Getwd()
    avformatPath := ""
    if runtime.GOOS == "linux" {
        os.Setenv("LD_LIBRARY_PATH", libPath+"/lib/linux")
        avformatPath = libPath + "/lib/linux/libavformat.so.58"
    } else {
        os.Setenv("LD_LIBRARY_PATH", libPath+"/lib/window")
        avformatPath = libPath + "/lib/window/avformat-58.dll"
    }

    fmt.Println("avformatPath=", avformatPath)
    avformatDllOnce.Do(func() {
        avformatDll = dylib.NewLazyDLL(avformatPath)
    })
    //avformatDll.NewProc("")
}

编译后
avformatPath= /home/go/1/fftest/lib/linux/libavformat.so
dlopen err: libavcodec.so.58: cannot open shared object file: No such file or directory
会报这个错误。
因为libavformat.so库涉及其他模块
我感觉我的问题是要把so库所有关联的库都要链接起来。当时不知道怎么实现。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

可以尝试两种方式

  • export LD_LIBRARY_PATH=/home/go/1/fftest/lib/linux/:${LD_LIBRARY_PATH}
  • echo "/home/go/1/fftest/lib/linux/" >> /etc/ld.so.conf
    ldconfig
    

简单解释来说libavformat.so依赖libavcodec.so.58,而libavcodec.so.58找不到导致这么个错误,详细参考 https://blog.csdn.net/callinglove/article/details/49682063