简体   繁体   中英

Cannot Compile C Program That Uses a Library (FFmpeg) with GCC Because of the Library's Include Statements

I am unable to compile a C project that uses a library called "FFmpeg" with a compiler called "GCC", and I believe it might be either because I don't quite understand how #include works or because I am using the wrong compilation process.

In a folder called Test , I have a file Test/test.c with the following contents:

#include <stdio.h>
#include <stdlib.h>
#include "FFmpeg/libavcodec/avcodec.h"

The folder FFmpeg is located at Test/FFmpeg . When I try to compile this with GCC, I receive the following error:

fatal error: libavutil/samplefmt.h: No such file or directory

The file Test/FFmpeg/libavcodec/avcodec.h has the following code in it:

#include "libavutil/samplefmt.h"
#include "libavutil/attributes.h"
#include "libavutil/avutil.h"
... //many more #include statements

Is the issue here that I need to add " FFmpeg/ " to all of these include statements?

If so, is there a way to automatically do this? This library is enormous and probably has hundreds of these statements.

If not, what should I be doing instead? Should I attempt to compile the library by itself? If so, how do I then include this compiled version of the library in my program?


Notes:

  • The command I am using to compile is gcc -c test.c .
  • I have GCC installed via MinGW.
  • I ultimately need to be able to compile this program to both a .dll and an .so .

I apologize if any of the terminology I use here is incorrect or if my explanations are poor. I know almost nothing about compilation. Please let me know if I need to fill in more information.

When #include is used with quotation marks (eg #include "file path here" ), it will read that file path as a relative file path .

In the case of compiling a C program using GCC, file paths are relative to the current directory. The "current directory" is the one into which you have placed your command prompt using the cd command .

In my case, I cd 'd into C:/Users/User/Documents/Test , meaning that all relative file paths are relative to C:/Users/User/Documents/Test . So when my compiler read

#include "libavutil/samplefmt.h"

it basically tried to do this:

#include C:/Users/User/Documents/Test/libavutil/samplefmt.h

when I instead needed the compiler to look at …/Test/FFmpeg/libavutil/samplefmt.h .

It turns out that the solution to this is to give the compiler additional locations to which relative paths might be relative. This is done with the -I[file path here] argument when you compile.

In my case, the way I needed to use this idea was to add C:/Users/User/Documents/Test/FFmpeg as a location to which paths might be relative. Thus, I could have taken my compile command:

gcc -c test.c

And inserted this:

gcc -IC:\Users\User\Documents\Test\FFmpeg -c test.c

However, this is actually an extremely clunky solution. There is a much easier way: it turns out that these file paths you provide with the -I argument can be relative to your current directory themselves . In my case, because my current directory in the command prompt was already C:/Users/User/Documents/Test , I could simply remove this portion from the above command, shortening it to this:

gcc -IFFmpeg -c test.c

And this solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM