为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
心有法竹 2022-04-26 10:29:21
我有一个最小的 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: -lavformatimport "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。这是在 Mac 上使用 ffmpeg 并来自自制软件:ffmpeg version 4.2.1Copyright (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() 之前运行了一些东西?
查看完整描述

1 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

问题是函数名open。它掩盖了系统调用打开,go 必须在它进入 main 之前调用。



查看完整回答
反对 回复 2022-04-26
  • 1 回答
  • 0 关注
  • 156 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号