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

问题描述 投票:0回答:1

我有一个基本的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()之前运行某些东西?

go cgo libav
1个回答
1
投票

问题出在函数名称open。它掩盖了打开的系统调用,必须先调用go才能到达main。

© www.soinside.com 2019 - 2024. All rights reserved.