用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库所有关联的库都要链接起来。当时不知道怎么实现。
可以尝试两种方式
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