This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I add a directory to C header include path?
I have run into this problem several times, and I thought it would be good to get an explanation from an experienced C or C++ programmer. In the example below I use a sample library name, but the actual name could be anything.
Say I want to run the following MWE:
#include <stdio.h>
#include <mylib/mylib.h> /* Also try mylib2/mylib/mylib.h */
int main (int argc, char **argv) {
printf ("Hello, World!\n");
return 0;
}
Further, assume that I have a Linux distribution that, instead of placing mylib.h in /usr/include/mylib/, chooses the directory /usr/include/mylib2/mylib/. So the straightforward command:
$ gcc test.c
would fail with the error:
fatal error: mylib.h: No such file or directory
But I might try to fix the include statement by writing:
#include <mylib2/mylib.h>
This gets me past the first error. But internally, mylib.h refers to other header files in the "usual" way: #include <mylib/anotherheader.h>. So now, even after I have fixed the original include statement in the MWE as #include <mylib2/mylib/mylib.h> the build breaks because mylib.h is attempting to include #include <mylib/anotherheader.h> instead of #include <mylib2/mylib/anotherheader.h>.
What is the standard way around this problem? There must be a simple compilation flag that I am missing. Thank you in advance.
You set up the include path so that it contains /usr/include/mylib2, usually with a command line option to the compiler like -I/usr/include/mylib2.
Note that usually an header includes related files from the same package using the
#include "another.h"
syntax, so that it is first searched in a relative to where it is installed. If your library had done this, there would be no problem with
#include <mylib2/mylib/mylib.h>
but even if it was the case you don't want your code to depend on where the libraries you use are installed. That dependence should be kept in the build system.
See the -I compiler flag on gcc and g++ it allows you to specify additional include paths:
http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
In this case you could specify:
-I /usr/include/mylib2
and #include "mylib/mylib.h" everywhere
Also, this is not part of your question but I thought I would throw it in there anyways, but there is a slight difference between include statements with quotes, and angle brackets:
http://msdn.microsoft.com/en-us/library/36k2cdd4(v=vs.80).aspx
I would recommend the following pattern:
/usr/libs/lib15 - is the directory where your library resides.
In the code:
#include <stdio.h>
#include <mylib1.h> // Header from your lib.
#include <mylib2.h> // Other header from your lib.
#include <mygraphs/defines.h> // Headers in your lib have even subdirectories.
In the command line:
cl -I /usr/libs/lib15 .....
You specify the search path for your headers.
Related
This question already has answers here:
Why should I not include cpp files and instead use a header?
(14 answers)
Closed 3 years ago.
As i said in the title i want to know if is an error to include .cpp files in their respectively .h
I'm compiling using cygwin on windows. The g++ compiler was installed in cygwin using apt-cyg install g++.
Then i added the cygwin path to env and now the g++ command is reachable from a windows cmd.
Here's a basic example to reproduce the problem.
main.cpp
#include <iostream>
#include "tezt.h"
int main(int argc, char const *argv[])
{
tezt();
return 0;
}
tezt.h
#ifndef TEZT_H
#define TEZT_H
void tezt();
#endif
tezt.cpp
#include <iostream>
#include "tezt.h"
void tezt()
{
std::cout<<"tezt"<<std::endl;
}
If i compile using simply g++ main.cpp i get errors.
So in order to compile correctly i should type g++ main.cpp tezt.cpp or define libraries.
It can be a trivial task when managing with multiple .cpp files stored in different folders.
So i'm proposing this approach:
if i modify the content of tezt.h, like follow, compilation is successfully! It can be a good "workaround" or it's not recommended? It's a colossal stupidity?
#ifndef TEZT_H
#define TEZT_H
#include "tezt.cpp"
void tezt();
#endif
Thanks for the attention.
Generally it is a bad idea to have an implementation in the header file.
if you include tezt.h file in different files in the same project it may have compilation error, with multiple definitions for the same symbol.
Including *.cpp works.
But the real solution is to use the tool make.
Create a makefile, which defines the dependencies, then call make, which then calls the compiler appropriatly.
I have a class called Timer that contains, obviously, code to track running time. I want to include this class in another project but I can't figure out how.
I have tried using
#include "Timer.h"
as well as using the file path to the project with the timer class, i.e.
#include "/users/user/projects/TimerProject/timer.h"
But that hasn't worked either, it tells me the file can’t be found. Is there something I am missing here?
Yes. You need to tell your C++ compiler where to search for include files. For gcc or clang, this is the -I command-line switch. So, for example:
g++ -o foo foo.cpp -I/users/user/projects/TimerProject/
and this will allow you to use:
#include <Timer.h>
Using double-quotes around the include name tells the compiler: "Search the same directory as the including file first, then search the include folders the compiler knows about". So if you have a foo.h next to your foo.cpp, you could use:
#include "foo.h"
without adding anything to the include path for it.
Finally: Files are case-sensitive on many operating systems. In your example, you have Timer.h and timer.h - make sure you use the correct spelling!
See also:
What is the difference between #include <filename> and #include "filename"?
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.
I'm fairly new to programming using linux so forgive me for any dumb errors I might make in my question but basically I am trying to compile using the terminal (C++) and my code in a .txt file however I keep getting a fatal error that my header file can't be found? When I try to type
g++ -o test main.cpp header.h
I get the error stating "header.h: no such file or directory" in the terminal. I've ensured that both the cpp and header files are in the same directory but no luck there. I've also used
#include <"header.h">
in my main.cpp and header file to try different fixes. I've researched and looked at different answers but no fixes either. Any suggestions?
#include <"header.h">
Use either
#include <header.h>
will lookup the standard include directories for these header files first
or
#include "header.h"
will lookup all include directory pathes specified with the preprocessor options
but don't mix these up.
Also you don't need to specify the header in the compiler command line
g++ -o test main.cpp header.h
# ^^^^^^^^ omit this
That's what the #include statement in your code is for.
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".