go - 链接到libavformat的最小Cgo应用程序意外执行

标签 go cgo libav

我有一个最小的C程序

#include <libavformat/avformat.h>

AVFormatContext *open(const char *url) {
    printf("URL %s\n", url);
    AVFormatContext *ctx = NULL;
    int err = avformat_open_input(&ctx, url, 0, 0);
    return ctx;
}

int main(int argc, char **argv) {
    open(argv[1]);
}

它有效,它将打印我传入的文件路径,并返回有效的AVFormatContext。

我将代码粘贴到Golang程序中:
package main

// #include <libavformat/avformat.h>
//  AVFormatContext *open(const char *url) {
//  printf("URL %s\n", url);
//  AVFormatContext *ctx = NULL;
//  int err = avformat_open_input(&ctx, url, 0, 0);
//  return ctx;
//  }
//  #cgo LDFLAGS: -lavformat
import "C"
import (
    "fmt"
    "os"
)

func main() {
    fmt.Println("Lets try this")
    url := os.Args[1]
    C.open(C.CString(url))
}

这将打印URL /dev/urandom(无论我给它什么参数)并挂起。非常奇怪的是,它不打印Lets try this
这是在使用ffmpeg的Mac上,并通过自制程序进行:
ffmpeg version 4.2.1
Copyright (c) 2000-2019 the FFmpeg developers   
built with Apple clang version 11.0.0 (clang-1100.0.33.8)

go version go1.13.4 darwin/amd64

我目前的猜测是编译器ABI不兼容,还是libavformat在main()之前运行某些东西?

最佳答案

问题出在函数名称open。它掩盖了打开的系统调用,该go必须在到达main之前进行调用。

关于go - 链接到libavformat的最小Cgo应用程序意外执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59296106/

相关文章:

mongodb - 为什么我的更新函数返回新查询但不更新数据库?

c - Golang (cgo) - 支持带有 cgo 的嵌套结构?

go - 在 Go 中使用 c 风格迭代器的惯用方式

php - 使用 php 或 avconv 获取 .mov 视频旋转

c++ - 使用 libavcodec 解码音频并使用 libAO 播放?

go - 如何在main.go中找到run函数的入口?

go - 来自外部包的 AWS Lambda golang 日志记录

go - 最小正float64值

algorithm - 使用 cgo 时;性能开销在哪里/什么时候?

c - swr_convert float 平面到 S16