c - fatal error :libavcodec/avcodec.h No such file or directory

标签 c ubuntu makefile ffmpeg

我尝试在我的ubuntu1604平台上调试ffmpeg,但它总是说找不到头文件,但实际上我确实在编译代码中包含了头文件。

错误信息如下: error message

这是我的测试代码:

 #include <stdio.h>
 #include "libavcodec/avcodec.h"
 #include "libavformat/avformat.h"
 #include "libswscale/swscale.h"

int main(int argc, char * argv[]){
av_register_all();
AVFormatContext *pFormatCtx = NULL;

// Open video file
if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) !=0 )
    return -1; // Couldn't open file

// Retrieve stream information
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
    return -1; // Couldn't find stream information

// Dump information about file onto standard error
av_dump_format(pFormatCtx, 0, argv[1], 0);

int i;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;

// Find the first video stream
int videoStream = -1;
for(i=0; i<pFormatCtx->nb_streams; i++)
    if (pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
        videoStream = 1;
        break;
    }

if (videoStream == -1)
    return -1; // Didn't find a video stream

// Get a pointer to the codec context for the video stream
pCodecCtx = pFormatCtx->streams[videoStream]->codec;

AVCodec *pCodec = NULL;
// Find the decoder for the video stream
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec==NULL){
    fprintf(stderr, "Unsupported codec!\n");
    return -1; // Codec not found
}   

// Copy context
pCodecCtx = avcodec_alloc_context3(pCodec);
if (avcodec_copy_context(pCodecCtx, pCodecCtxOrig) !=0 ){
    fprintf(stderr, "Couldn't copy codec context");
    return -1; // Error copying codec context
}

// Open codec
if (avcodec_open2(pCodecCtx, pCodec, NULL)<0)
    return -1; // Could not open codec

AVFrame *pFrame = NULL;
// Allocate video fream
pFrame = av_frame_alloc();

// Allocate an AVFrame structure
AVFrame *pFrameRGB = NULL;
pFrameRGB = av_frame_alloc();
if(pFrameRGB == NULL)
    return -1;
}

以下是我的 Makefile

C++     = gcc
INCLUDE_DIR = ./include
LIB_DIR     = ./lib
BIN_DIR     = ./build/bin
TARGET      = $(BIN_DIR)/mk-screen-caps

INC_PATH    += -I./ -I$(INCLUDE_DIR)
LIBS        += -lstdc++ -lm -lgcc_s -lgcc -lc
LIBS        += $(LIB_DIR)/libavcodec.a $(LIB_DIR)/libavdevice.a 
$(LIB_DIR)/libavfilter.a 
LIBS        += $(LIB_DIR)/libavformat.a $(LIB_DIR)/libavutil.a 
$(LIB_DIR)/libfdk-aac.a 
LIBS        += $(LIB_DIR)/libmp3lame.a $(LIB_DIR)/libopus.a 
$(LIB_DIR)/libpostproc.a 
LIBS        += $(LIB_DIR)/libswresample.a $(LIB_DIR)/libswscale.a 
$(LIB_DIR)/libx264.a 
LIBS        += $(LIB_DIR)/libx265.a $(LIB_DIR)/libyasm.a

C++_FLAGS   += -pipe -g -Wall -o0 

C_FILES     += $(wildcard ./*.c);
CPP_FILES   += $(wildcard ./*.cpp)
HEAD_FILES  += $(wildcard $(INCLUDE_DIR)/*.h)

C_OBJS=$(C_FILES:.c=.o)
CPP_OBJS=$(CPP_FILES:.cpp=.O)

all: $(TARGET)
$(TARGET): $(CPP_OBJS)
$(C++)  $(C++_FLAGS) -o $@ $(CPP_FILES) $(LIBS)

%.o: %.c $(HEAD_FILES)
    $(C++) -c $(C_FILES) $(INC_PATH) $< -o $@
%.O: %.cpp $(HEAD_FILES)
    $(C++) -c $(CPP_FLAGS) $(INC_PATH) $< -o $@

clean:
    rm -f $(TARGET) $(C_OBJS) $(CPP_OBJS)

以下是我的目录信息:

mk-screen-caps(父目录)

--构建

--bin

--包括

--(需要包含文件和目录)

--lib

--(需要的库)

最佳答案

根据发布的错误消息,您的 INC_PATH Make 变量未使用。因此,gcc 找不到 avcodec.h

也许您已经更改了 Makefile,因为如果我运行上面发布的文件,它实际上使用了模式规则 %.O: %.cpp $(HEAD_FILES)

尝试运行make --debug。对我来说,这会导致:

$ make --debug
GNU Make 4.1
Built for aarch64-unknown-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Reading makefiles...
Updating goal targets....
 File 'all' does not exist.
   File 'build/bin/mk-screen-caps' does not exist.
     File 'test.O' does not exist.
    Must remake target 'test.O'.
gcc -c  -I./ -I./include test.cpp -o test.O
test.cpp:2:33: fatal error: libavcodec/avcodec.h: No such file or directory
compilation terminated.
Makefile:31: recipe for target 'test.O' failed
make: *** [test.O] Error 1

它也失败了,因为我在任何目录.include中都没有libavcodec/avcodec.h。但可以看出,include目录列表实际上是在gcc的调用中使用的。

关于c - fatal error :libavcodec/avcodec.h No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49923107/

相关文章:

windows - 使用环境变量识别 Cygwin、Linux、Windows

c - "Inline C"- 问题

c++ - 编译 Boost 链接库 (Ubuntu)

ubuntu - 连接到 WSL/Ubuntu 20.04 LTS 时远程桌面连接崩溃

c - 无法运行编译后的文件 - bash : ./a.out : Permission denied.(我试过 chmod)

python - 我应该把 portaudio 放在哪里以便 Pyaudio 可以找到它

linux - Makefile 中的 Chroot 语法

objective-c - 数组的大小 - iOS

c - Cortex-M4F MCU 启动中关键部分的内存屏障

c - 如何避免为此操作在 C 中分支