我有一个最小的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/