Cgo“未定义参考”gstreamer

问题描述

我正在使用cgo编写一些东西来与gstreamer-1.0库进行交互。我的所有工作几乎都可以正常运行,但是由于某种原因,整个头文件的对象无法正确导入。

go version go1.15.2 linux/amd64进行任何有价值的操作

package main

// #cgo pkg-config: gstreamer-1.0
// #cgo CFLAGS: -Wno-deprecated-declarations
// #include <gst/gst.h>              // using this file extensively with no issues
// #include <gst/app/gstappsink.h>   // objects in this file are not getting read,but the compiler is having no issue reading it
import "C"

func init () { C.gst_init(nil,nil) }

func main () {
  // ...
   C.gst_app_sink_pull_sample()  // a non-variadic function that does take args
                                 // but cgo doesn't even think it exists.
  // ...
}

从编译器返回的错误/tmp/go-build/cgo-gcc-prolog:64: undefined reference to 'gst_app_sink_pull_sample'

我已经查看了头文件,并且确实存在gst_app_sink_pull_sample。我既可以尝试在本地也可以在golang docker容器中进行构建。

如果我完全删除include,则错误将有所不同:Could not determine kind of name for C.gst_app_sink_pull_sample

那么我是问题还是gstreamer问题?

解决方法

appsrc和appsink符号不属于基本gstreamer库。而是在额外的库gstreamer-app-1.0中找到它们。将此库添加到您的cgo pkgconfig行中,它应该找到缺少的符号。

,

“对xxx的未定义引用”表示cgo的C编译器可以识别定义,但是找不到实现(由相应的C库覆盖)

这表明您已正确导入C头文件。要解决未定义的引用问题,如果动态库名为libgstreamer.so.1.0.0

,则只需添加以下内容
# cgo LDFLAGS: -lgstreamer