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

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"

Related

Can't link a static binary to #include it

Good day,
I have a file that I'm trying to compile and within it has an #include to a statically linked binary.
#!/bin/bash
g++ -Wall -std=c++17 Message.cpp ../textmagic-rest-cpp/lib/libtextmagic.a
I am getting the following error: fatal error: libtextmagic.h: No such file or directory
The relative path that I provided is correct under the assumption that the current working directory is the directory in which the script is called/ran. I might be linking the binary incorrectly and I've searched around the internet but the other posts/resources did not help me.
Note that the script is run in the same directory as Message.cpp.
g++ has the -I and -L flags that do that for you. Your flag will look like this: -I/ThePathToYourHeaders and -L/ThePathToYourLib. I don't know if g++ supports relative paths there but absolut paths are guaranteed to work there.
Also you probably need to add a linker flag. For your project it will be -ltextmagic. It is just the name of the .a file you want to link with, without the lib in front of the filename.
The #include directive needs to "read" the header file you give it as argument, and that is not included in the static library.
You can either include using a relative path to the source file or pass the location of the header file to the compiler using the -I argument.

cygwin: no header file found

The following problem: Cygwin or Visual Studio give the error.
CGAL/Splitters.h: No such file or directory
This error appears for every header! The header is implemented by the code
#include <CGAL/Splitters.h>
The error disappears, if I change the code, such that I write the whole path:
#include <c:/path1/path2/CGAL/Splitters.h>
But this is no solution which satisfies me, because I would have to change hundreds of such code fractions.
I think it should be a problem of Visual studio or cygwin. In cygwin I wrote the command:
$ g++ -std=c++11 example.cpp -o example
What is the reason for the error? How can I fix it?
Please give easy understandable instructions, since I am a beginner in C++.
The compiler does not know where to look for the header file
referred to by #include <CGAL/Splitters.h> unless you tell it,
because that header file is not located in any of the compiler's
default search directories for header files.
You tell the compiler where to look by passing it an -I option:
$ g++ -I/path/to/cgal/headers -std=c++11 example.cpp -o example
where that header file will be:
/path/to/cgal/headers/CGAL/Splitters.h
Further reading: An Introduction to GCC - for the GNU compilers gcc and g++
Later
So for every of the header files I have to write the - I option?
No. -I/path/to/cgal/headers/ by itself will of course tell the compiler
where to find every CGAL header file used in your program.
You can check include path in Visual Studio :
Tools > Options > Projects and Solutions > VC++ Directories > Include files
So, check default header file path.
I hope this can help you.

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 compiler cannot find hpp files

I am trying to install the hep-mc library listed here: https://github.com/cschwan/hep-mc for use on compute using the instructions listed in the documentation here: https://github.com/cschwan/hep-mc#installation . To compile one of the example files, I typed this into the terminal:
g++ -L/usr/local/hep-mc/include vegas_mpi_ex.cpp -o vegas_mpi
but I get these error messages:
mpi_vegas_ex.cpp:1:22: error: hep/mc.hpp: No such file or directory
mpi_vegas_ex.cpp:2:26: error: hep/mc-mpi.hpp: No such file or directory
mpi_vegas_ex.cpp:8:17: error: mpi.h: No such file or directory
in the beginning of my code, the declarations are like this:
#include "hep/mc.hpp"
#include "hep/mc-mpi.hpp"
#include <mpi.h>
The tutorial states that I should point the compiler to the location of the "include" folder that contains all the .hpp files, which I have done. Do you guys have any idea as to what I'm doing wrong?
It should also be noted that the compiler cannot find the mpi.h directory even though I have loaded the openmpi module.
-L sets paths where the linker searches for libraries to link. The option you're looking for is -I, which sets the paths where the compiler searches for #included files.
g++ -L/usr/local/hep-mc/include vegas_mpi_ex.cpp -o vegas_mpi
Oops!
g++ -I/usr/local/hep-mc/include vegas_mpi_ex.cpp -o vegas_mpi
-L specifies the path to library files; -I specifies the path to includes.
This is confusing because in terms of project management and distribution, we consider "the library" to include both binaries and header files, as well as documentation and all sorts of goodies. But at a technical level that is not what "library" means.

Lzz (Lazy C++) - #include file not found

I am trying to use Lzz to generate C++ header files from my *.cpp files.
The calling sequence is something like this:
./lzz -hx hpp -c -o out src/*.lzz
Unfortunately, it always fails saying it couldn't find any included headers, including parts of the standard library such as iostream, string and vector.
I get a bunch of error messages like these:
src/CommonIO.lzz:7:10: #include file not found.
src/CommonIO.lzz:8:10: #include file not found.
src/CommonIO.lzz:9:10: #include file not found.
I know I can do give it include paths using the -I parameter:
-I /usr/local/include/c++/4.5.1
but it does not seem to help, because it starts complaining about includes from the standard library:
/usr/local/include/c++/4.5.1/string:40:10: #include file not found.
Could it be caused by the fact that I am running the 32-bit version (binary downloaded from http://www.lazycplusplus.com/download.html) on a 64-bit system (Ubuntu 10.10)?
I have already tried to compile my own Lzz from the source, but It complains about missing rule for making libconf.a:
make[1]: * No rule to make target /home/petmal/Desktop/Downloads/lzz_2_8_2_src/gcc.opt/libs/libconf.a', needed by/home/petmal/Desktop/Downloads/lzz_2_8_2_src/gcc.opt/lzz'. Stop.
Enclose your #includes with
#hdr
...
#end
The delimited lines are copied verbatim to the header file.