gcc: Using a function from another directory - c++

I would like to use a function from another directory. So I put this line on the top,
#include "~/ffmpeg/include/libavcodec/avcodec.h"
and ran
gcc filename.c
and getting this error
error: avcodec.h: No such file or directory
The spelling is correct and the file exists. Why does the compiler think the file does not exist?

Use:
#include <avcodec.h> /* NOT "avcodec.h" */
Then:
gcc -I/include/file/path/where/avcodec.h/lives
If you use -I, use the angle-brackets, if you want to include based on a path relative to the file containing the #include, use the quotes.
I would use:
#include <libavcodec/avcodec.h>
and:
$ gcc -I$HOME/ffmpeg/include ...

#include "~/ffmpeg/include/libavcodec/avcodec.h"
Is using ~ which is a HOME identification for KSH and BASH only.
You need to use a full/relative path as include.

A preferred way to do this - which makes it so that code doesn't have to change simply because a header is in a different directory, is to place in the code:
#include <avcodec.h>
Then when you are compiling add the switch -I
gcc -I/home/<username>/ffmpeg/include/libavcodec
Its best to use the full path to the header.

You need to tell gcc where the include directory is using -I and where the shared libraries are with -L
gcc -I~/ffmpeg/include -L~/ffmpeg/lib filename.c
an change the include to
#include "libavcodec/avcodec.h"

Related

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 (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.

Multiple includes with #include in C++

I have a question regarding the #include preprocessor directive in C++.
In my program I would like to include a header file from another directory. For this I have used the full path, example:
#include "full/path/to/my/header.hpp"
Now it turns out that header.hpp itself has an include (let's say #include "otherheader.hpp". During compilation, the compiler complains that it cannot find this other header.
What is the best way to handle this problem, considering the fact that I also don't want to write the full path for every header file, especially including those that are only needed "further down the tree"?
You should use your compiler's -I option.
Example with g++:
g++ -I full/path/to/my/
Then in your code you can simply put:
#include "header.hpp"
More information on search path.
Most compilers allow you to specify the root for additional include directories when compiling. For example, in Visual C++ you specify the -I flag. It's also the same for gcc. For example:
compiler -Ipath/to/headers myfile.c
Don't specify full path to includes. Specify include directories to the compiler instead and then just #include <foo> knowing that foo will be found in /some/path/foo because you told the compiler to search /some/path/ for includes. For many compilers this is done with the -I option.

How to run C++ program using GCC?

I need to run a demo program, ba_demo.cpp, which is in a folder structure, "C:/root/demo_parent1/demo_parent2/demo.cpp", and this file uses the following code in it.
#include "root/sub1/sub2/header_file.h".
The file is at
'C:/root/sub1/sub2/header_file.h'
When I try to compile the demo program by the command
C:\root\demo_parent1\demo_parent2> gcc demo.cpp
it is not finding the header file and is throwing an error. What changes should I make to my command in order to run the demo program successfully?
First you must inform your compiler where to look for root directory by adding flag -IC:
so your command will look like this:
C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:
But that's not practical. Better change include to
#include header_file.h
and add only /root/sub1/sub2/ directory, which will look like this:
C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:\root\sub1\sub2\
Now you have your program compiled to a.out file. You execute it by
C:\root\demo_parent1\demo_parent2>./a.out
You can change output name to demo.exe like this:
C:\root\demo_parent1\demo_parent2> g++ demo.cpp -IC:\root\sub1\sub2\ -o demo.exe
Please note that since you work on Windows under some &nix simulator the slashes in my examples might be wrong but I can't check them now. Use google to check how to handle them properly.
EDIT: Also, don't forget to take Lukas Holt's advice and use g++! It is very important to use proper compiler for proper language.
Your .cpp file is in: C:/root/demo_parent1/demo_parent2/demo.cpp
You include:
#include "root/sub1/sub2/header_file.h"
This is a relative path. You need either to specify an include directory with the -I flag, or to move header_file.h to C:/root/demo_parent1/demo_parent2/root/sub1/sub2/header_file.h.
The most simple solution would be to just #include "header_file.h" and to move it into the same directory as demo.cpp.
Then you will be able to compile it with gcc, and to execute the generated file.

g++ can't find headers even when it's specified

So basically I have some really simple code that includes <BigIntegerLibrary.hh> which resides in /Users/wen/Projects/include/bigint. I was compiling with this:
g++ main.cpp -o Main -I/Users/wen/Projects/include/bigint
but it reported a fatal error that it could not find the file. Am I doing it right? Thanks!
main.cpp:4:10: fatal error: 'BigIntegerLibrary.hh' file not found
Try
#include "BigIntegerLibrary.hh"
If you specify the #included file with angle brackets (#include <includeFile.h>) the compiler will try to find it in a predefined location whereas if you use #include "includeFile" the compiler first tries the paths you specified with the -I compiler option.
The -I compiler option cannot be used to specify where the <...> files are.
If the path is correct g++ should see the files.
If you use absolute path in include directive, you should change quotation:
#include "/Users/wen/Projects/include/bigint/BigIntegerLibrary.hh"

in include if i use "test.h" is the same with "path/test.h"?

I am working in ubuntu under c++ language.
I have a question: i use #include"header.h". Is this the same with /path/header.h? I ask you this question because as I've seen is not the same thing. Need some explications.
I ask you this question because I've downloaded and install gsoap on my computer. I added all the necessary dependencies in a folder and I've tried to run the app without installing gsoap ...on a different computer. I had some errors..i forgot to add stdsoap2.h file...I will add it today..in my folder..
The answer is it depends:
If you have "path/" added to your include path then including only "header.h" will work because then compiler already knows the path to lookup for your header files, if not
then you have to include entire path "path/header.h" so the compiler knows where to look for the header file.
If header.h is in the directory path/, then #include "header.h" will work for those header and source files (which #include header.h which happen to be in the same directory as header.h (path/).
On the other hand, if you are #include-ing header.h in a file that is in a different directory than path/, then the above way would not work. To make it work, you can try 2 different approaches:
#include the complete path to header.h. Your #include will look something like:
#include "path/header.h"
Include the path/ directory to the makefile. This will get g++ to look for header.h in those directories as well. This can be done like so (in the makefile):
g++ <some parameters> -Ipath/ -c main.cpp -o main.o (assuming header.h is called from within main.cpp). If you choose this way, then the #include will also change, like so:
#include <header.h>. Note the use of the -I flag as a parameter to g++. That flag tells g++ to look into additional directories as well.
No, they are not the same, conceptually. The results, however, could be the same. It depends on how you tell your compiler to find headers (the -I flag in g++). If you would compile with -I /path/, then you'd find /path/header.h with #include "header.h". If you do not use that include path flag, then you'd have to write #include "/path/header.h".