NetherGranite
NetherGranite

Reputation: 2100

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:


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.

Upvotes: 1

Views: 1864

Answers (1)

NetherGranite
NetherGranite

Reputation: 2100

When #include is used with quotation marks (e.g. #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 alreadyC:/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.

Upvotes: 2

Related Questions