Header files not found in c++, possible path issue - c++

I have a file structure like this:
main.cpp --> #include <headers/f1/v.h>
headers/f1/v.h --> #include <headers/f1/i.h>
headers/f1/i.h
headers is a directory of external library.
Compiled with 'g++ main.cpp' and got file not found error:
In file included from main.cpp:11:
./headers/f1/v.h:32:10: fatal error: 'headers/f1/i.h' file not found
#include <headers/f1/i.h>
Very new to c++. Really can't figure it out. What has been wrong here? Thanks!

When including your own headers, in the same build tree, you should use quotes not angle brackets:
#include "headers/f1/v.h"
If you do get into the situation that you need <> for local files, for whatever reason, you could add the directory to your compiler's include path:
g++ main.cpp -I .
where . is the POSIX convention for "this directory".
Further reading:
What is the difference between #include <filename> and #include "filename"?

Related

Header Files Not Found (Wrong Path?)

I am trying to compile the a program in Linux and the program contains the following header files:
#include <iostream>
#include <vector>
#include "Minuit2/FCNBase.h"
#include "FunctionMinimum.h"
#include "MnMigrad.h"
etc. The source file is in
home/christian/code
and the header files are all in
/home/christian/root/include/Minuit2
I am trying to compile by running the following command:
g++ -I /Minuit2 niminimzationExample.cpp -o niminimzationExample -L/Minuit2/lib -lMinuit2
But I get the following error message:
In file included from niminimzationExample.cpp:9:0:
/home/christian/root/include/Minuit2/FCNBase.h:13:10: fatal error: Minuit2/MnConfig.h: No such file or directory
#include "Minuit2/MnConfig.h"
Because the compiler cannot find MnConfig.h which is the first header file inside of FCNBase.h. I have also tried to run
g++ -I /home/christian/root/include/Minuit2 niminimzationExample.cpp -o niminimzationExample -L/Minuit2/lib -lMinuit2
But I still get the same error. What is the write way to include the header files?
Thanks.
If your header is at
/home/christian/root/include/Minuit2/FCNBase.h
Your #include or compile option is wrong.
Currently you are telling the compiler to search for
/Minuit2/Minuit2/FCNBase.h
or
/home/christian/root/include/Minuit2/Minuit2/FCNBase.h
You should specify an option
-I /home/christian/root/include
To have the compiler search for Minuit2/FCNBase.h in the directory /home/christian/root/include.
If you don't want to change the option, you should change the #include to
#include "FCNBase.h"
To have the compiler search for FCNBase.h in the directory /home/christian/root/include/Minuit2.

file not found error when trying to include id3lib header

I downloaded id3lib and placed the directory in my main.cpp directory but both g++ and visual studio give file/directory not founds and "undefined" errors
Here is my main.cpp:
#include <iostream>
#include <id3lib-3.8.3/include/id3/tag.h>
int main() { std::cout << "hi"; }
g++ main.cpp gives:
main.cpp:2:46: fatal error: id3lib-3.8.3/include/id3/tag.h: No such file or
directory
#include <id3lib-3.8.3/include/id3/tag.h>
if I use "" instead of <>, i get this error:
id3lib-3.8.3/include/id3/tag.h:32:30: fatal error: id3/id3lib_frame.h: No
such file or directory
#include <id3/id3lib_frame.h>
It's not enough to put it beside your main file. As you can see in your first approach when you used #include with <> it can't find it, that's because (copied from here) :
For #include <filename> the preprocessor searches in an implementation
dependent manner, normally in search directories pre-designated by the
compiler/IDE. This method is normally used to include standard library
header files.
You didn't tell your compiler where to look for id3lib-3.8.3/include/id3/tag.h so <> will not work for you.
Then you tried "". it found id3lib-3.8.3/include/id3/tag.h but in the tag.h there is #include <id3/id3lib_frame.h>, So back to problem with first approach, right?
What you need to do is that you need to tell your compiler/IDE where to look for these files. In visual studio click right on your project file, then properties->C/C++->General->Additional Include Directories and add the include library ($(ProjectDir)id3lib-3.8.3/include/ or maybe $(SolutionDir)id3lib-3.8.3/include/) to it. Then your first approach should work fine.

Header files not found even though they are in the right directory?

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.

the fatal error "fstream.h" file not found happen in Clion?

When I use the code #include <ofstream.h> in my .h file I get this error:
"fstream.h" file not found
I use the Clion in os x.
In C++ you should not include files (system-files), which ends with .h, instead do #include <fstream>
see: http://en.cppreference.com/w/cpp/io/ofstream

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"