No such file or directory despite using -I flag - c++

I want to run preprocessor and generate .i files using the following command
(I based on http://jkorpela.fi/html/cpre.html) :
gcc -Irange/v3/action -x c -P -C -E range/v3/action.hpp
but I have error:
range/v3/action.hpp:17:10: fatal error: range/v3/action/action.hpp: No such file or directory
#include <range/v3/action/action.hpp>
The command is called from include directory. Directories tree is shown below:
-include
-range
-v3
-action.hpp (file)
-action (directory)
-action.hpp
I've tried replace -I flag with other options, for example -Irange/**, but it did not work also.
I will be grateful for your solution or suggestion.

The path specified in #include is appended to the directory in -I. So it's looking for range/v3/action/range/v3/action/action.hpp. That's duplicating the directory path, because you have it in both -I and #include.
Either use -I. to start the search from the current directory, or use #include <action.hpp> to just search for the filename in the -I directory.

Related

How to specify location of angle-bracket headers in gcc/g++?

Is there a way to tell gcc/g++/clang where to look for headers that are included via angle brackets ("<", ">")?
I don't use the angle bracket convention for non-system files, but the problem is that when I try using the headers from some packages I download, I get errors for all of the included files.
For example, say I want to include headers from a module called Foo that I download:
/foo-v1.0/include/DependencyA.hpp:
#ifndef DEP_A_HPP
#define DEP_A_HPP
class DependencyA
{
...
};
#endif
/foo-v1.0/include/Api.hpp:
#ifndef FOO_HPP
#define FOO_HPP
#include <Foo/DependencyA.hpp>
void doSomething(DependencyA* da);
#endif
Then, in my own code:
/mycode.cpp:
#include "/foo-v1.0/include/Api.hpp"
DependencyA* da = new DependencyA();
doSomething(da);
I get a compile error:
fatal error: 'Foo/DependencyA.hpp' file not found
I've tried building with:
clang -c mycode.cpp -isystem./foo-v1.0/include -o mycode.o
clang -c mycode.cpp -isystem./foo-v1.0/include/ -o mycode.o
clang -c mycode.cpp -I./foo-v1.0/include -o mycode.o
clang -c mycode.cpp -I./foo-v1.0/include/ -o mycode.o
and so on, to no avail.
How do I tell the compiler to resolve <Foo/**/*> to a particular root directory for every included file?
The answer is already in the comments.
To check include dirs one can use the method described here: What are the GCC default include directories? , preferably with - replaced with /dev/null:
clang -xc -E -v /dev/null
On my machine for clang it gives
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/clang/11.0.0/include
/usr/include
End of search list.
To discover how to manipulate this list, it suffices to read the gcc (or clang) manual (man clang or find it in the Internet, for example, https://man7.org/linux/man-pages/man1/gcc.1.html ). For gcc this reads:
Options for Directory Search
These options specify directories to search for header files, for
libraries and for parts of the compiler:
-I dir
-iquote dir
-isystem dir
-idirafter dir
Add the directory dir to the list of directories to be searched
for header files during preprocessing. If dir begins with = or
$SYSROOT, then the = or $SYSROOT is replaced by the sysroot
prefix; see --sysroot and -isysroot.
Directories specified with -iquote apply only to the quote form
of the directive, "#include "file"". Directories specified with
-I, -isystem, or -idirafter apply to lookup for both the
"#include "file"" and "#include <file>" directives.
This description is followed by a detailed description of the order in which header files are searched and by some recommendations as to which option to use for which purpose. You'll find it in the manual. Search for "Options for Directory Search".
What I really don't like in your code is this line:
#include "/foo-v1.0/include/Api.hpp"
It seems to contain the absolute path to the header and I've never seen anything like this. I would change it to
#include "Api.hpp"
with /foo-v1.0/include being added to the search list via the usual compiler -I command-line option.

Trying to use -I option with g++

I am trying to compile a source file driver.cxx and among its include files is a library called
The path to this file is /home/terry/Downloads/libodb-2-4-0/odb/sqlite/database.hxx
to compile it I enter the following:
g++ -c driver.cxx -I/home/terry/Downloads/libodb-2.4.0/odb
And get the message
driver.cxx:10:35: fatal error: odb/sqlite/database.hxx: No such file
or directory #include
^ compilation terminated.
How do I mention the path when using the -I flag for g++?
According to the error you pasted it looks like your include command is:
#include "odb/sqlite/database.hxx"
If so, your -I option should be without odb dir (since it's already mentioned in the include):
-I/home/terry/Downloads/libodb-2.4.0/
All in all the -I concatenated with the include should be the exact path.
Meaning if you decide to include with:
#include "database.hxx"
Your -I option should be:
-I/home/terry/Downloads/libodb-2.4.0/odb/sqlite
Again, -I + include = exact path.
Since the error message mentions 'odb' part of the path I would remove it from -I flag
Let's say you want to use database.hxx in your .cpp file. Then in your .cpp file you should write:
#include "database.hxx"
and for compiling you should mention the path where the .h is present. So in your case it would be.
-I /home/terry/Downloads/libodb-2-4-0/odb/sqlite/
I can see that your error mentions you use #include <odb/sqlite/database.hxx>. Try to change it to #include <database.hxx>.

Where is the -I (captial i) path relative to in g++?

I am in the App folder of my project. I run the following command to compile character.cpp
g++ -Wall -std=c++11 -I../App -c Character/character.cpp -o Obj/character.o
which is in App/Character directory. character.cpp has the following include
#include "Inventory/inventory.hpp"
where the folder of inventory.cpp is App/Inventory.
I thought because I am running the g++ command from App, the default include path would start from App and therefore I wouldn't need to have the -I../App part of the command. To me this seems to be saying "move one level higher than App then move into App and include from there" which seems redundant but without that line it doesn't work.
Can anyone explain why?
EDIT
Looking at it again and some more documentation, I believe that if no -I path is specified, g++ will look in its default directories and then all other includes (like the one I have causing problems) are relative to the file that includes them. So I have to add the -I part to say "look in the App directory too" and since it doesn't like just -I, I have to use ../App because that is equivalent to not moving at all. Can anyone confirm if this is at all accurate?
You can use -I. for searching headers from the current directory, instead of -I../App.
This include preprocessor directive
#include "Inventory/inventory.hpp"
forces gcc (g++ or cpp) to search the header not from the current path (App/), but from directory of your source file (App/Character):
/root/App# strace -f g++ -c -H ./Character/character.cpp 2>&1 |grep Inven
[pid 31316] read(3, "#include \"Inventory/inventory.hp"..., 35) = 35
[pid 31316] stat64("./Character/Inventory/inventory.hpp.gch", 0xbfffe6a4) = -1 ENOENT (No such file or directory)
[pid 31316] open("./Character/Inventory/inventory.hpp", O_RDONLY|O_NOCTTY) = -1 ENOENT (No such file or directory)
..then try system directories
This is documented here: https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
GCC looks for headers requested with #include "file" first in the directory containing the current file
This behavior can be not fixed in the Language standard (ISO C), and is implementation-defined (as commented by Richard Corden and answered by piCookie in What is the difference between #include <filename> and #include "filename"?):
specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner.
But this is the way the C compiler should work under Unix, according to Posix, aka The Open Group Base Specifications Issue 7:
Thus, headers whose names are enclosed in double-quotes ( "" ) shall be searched for first in the directory of the file with the #include line, then in directories named in -I options, and last in the usual places. For headers whose names are enclosed in angle brackets ( "<>" ), the header shall be searched for only in directories named in -I options and then in the usual places. Directories named in -I options shall be searched in the order specified.
It is useful when your current directory is far from the source directory (this is the recommended way in autotools/autoconf: do mkdir build_dir5;cd build_dir5; /path/to/original/source_dir/configure --options; then make - this will not change source dir and will not generate lot of file in it; you can do several build with single copy of source).
When you start g++ from the App directory with -I. (or with -I../App or -I/full_path/to/App), gcc (g++) will find the Inventory. I added warning to the header to see when it will be included; and -H option of gcc/g++ prints all included headers with pathes:
/root/App# cat Inventory/inventory.hpp
#warning "Inventory/inventory.h included"
/root/App# cat Character/character.cpp
#include "Inventory/inventory.hpp"
/root/App# g++ -I. ./Character/character.cpp -H -c
. ./Inventory/inventory.hpp
In file included from ./Character/character.cpp:1:
./Inventory/inventory.hpp:1:2: warning: #warning "Inventory/inventory.h included"

How to apply C++ preprocessor to a C++ header file on Ubuntu

I tried to apply preprocessor to a C++ header file with Macros using the below command.
$ g++ -E heap.h
And I wasn't able to get the preprocessed header file, because preprocessing was finished with the below error.
...
...
# 9 "heap.h" 2
heap.h:10:28: fatal error: src/allocation.h: No such file or directory
To tell g++ about the directories that includes header file included in heap.h, I typed the below command, but it showed the same error.
$ g++ -E heap.h -I .
...
...
heap.h:10:28: fatal error: src/allocation.h: No such file or directory
Can you leave the solution of this problem if you have an experience that you solve this problem?
The steps:
You may need to change to the directory from where your build system invokes the command if the command does not use absolute paths.
Copy the compiler command line from your make/cmake/etc. output.
Add -E switch.
Add/change -o parameter to <source>.i.

c++ ERROR libxml++ no such file or directory

HI,
I have the following: #include <libxml++/libxml++.h> and when i compile it says fatal error: libxml++/libxml++.h.No such file or directory. I've checked in the directory: /usr/include/libxml++-2.6/libxml++ and there it is the libxml++.h. Where am I wrong? why do i receive this error? thx
EDIT:
I did include g++ prg.cpp -o prg -I/usr/include/libxml++-2.6/ and now i have the
error:fatal error: glibmm/ustring.h: No such file or directory
You should use pkg-config to get the correct compiler options. See, for instance:
http://developer.gnome.org/libxml++/stable/
Add the following option in the makefile:
g++ <some options> -I/usr/include/libxml++-2.6 <some other options>
The -I flag in g++ adds the directory appearing after it to the include path. If you do not want to use that option, you need to replace the #include<libxml++-2.6/libxml++.h> with #include "absolute path to above header file". Note that using the -I flag also allows you to replace " " after the #include with < > tags.
add -I/usr/include/libxml++-2.6/libxml++ when compiling.