Header Files Not Found (Wrong Path?) - c++

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.

Related

g++: Can I compile without including Boost if I include a PCH which includes Boost (/seems to include non-PCH)?

I have the following file, boost_pch.h. When compiling, I use g++ boost_pch.h -I C:\...\boost which successfully creates the file boost_pch.h.gch (g++ 9.2.0).
#ifndef BOOST_PCH_H
#define BOOST_PCH_H
#define BOOST_MP_DISABLE_DEPRECATE_03_WARNING
#include <boost/multiprecision/cpp_int.hpp>
#endif
However, including this in a .cpp file and compiling as g++ my_file.cpp ... causes an error on the #include <boost/multiprecision/cpp_int.hpp> ( fatal error: boost/multiprecision/cpp_int.hpp: No such file or directory) from boost_pch.h. It seems to be including the normal header file and not the PCH.
The first line of the .cpp file is
#include "boost_pch.h"
So is the problem that it's not detecting boost_pch.h.gch and trying to include boost_pch.h or that I can't precompile a header with additional includes and then expect to be able to include the PCH without adding the required includes?
Running g++ on my_file.cpp with the include path for boost added compiles and works fine, but it takes ages to compile and is exactly what I'm trying to avoid.

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

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"?

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.

gcc: Using a function from another directory

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"

(Compiler error) Why does C++ does not find the .h file even though its there?

I am trying to run a C++ file, namely experiment.cpp. On compiling the .cpp file, I get an error that RL_glue.h does not exist, even though RL_glue.h is in the same directory as the C++ file. I kindly call for your suggestions on this issue. Thanks!
The include is as follows:
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <RL_glue.h>
#include <RL_common.h>
Though I changed it as suggested in the comment:
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "RL_glue.h"
#include "RL_common.h"
The compiler I am using is gcc 4.6.3 and the compilation command is g++ -Wall -c "%f"
Correction: With the " .... " change, the compiler found RL_glue.h, however, it peculiarly failed in finding RL_common.h.
The exact compiler error:
g++ -Wall -c "experiment.cpp" (in directory: /home/issam/Helicopter_Control/Code/Release)
In file included from experiment.cpp:9:0:
./RL_glue.h:4:23: fatal error: RL_common.h: No such file or directory
compilation terminated.
Compilation failed.
Note the compiler error location:
./RL_glue.h:4:23
The error is in RL_glue.h including RL_common.h, not in your include of that file. That's because in line 4 of the file RL_glue.h there is something like:
#include <RL_common.h>
instead of:
#include "RL_common.h"
You can replace that line, or even easier is to add -I. to the compiler option, so that the current directory . is searched no matter the include style.
The rules for finding included files vary with the compiler, and you didn't say which one you use so it is hard to tell... but I'll try anyway.
Usually, when you use #include <file> it is searched in the system directories and when you write #include "file" it is searched in the project directories.
My guess is that you are using the <> style when you should be using the "" syntax.
If that doesn't work you may need to add the current directory to the search path, usually with the -I. compiler option, or if you use an IDE, searching in the project options.