g++ include directory in the headerfile - c++

The path to my .cpp and .h files: /home/quasiturbine/ServerProject/Network/NetworkIncludes/
There you can find TCP_Connexion.h and TCP_Connexion.cpp
In the .cpp file, I got #include "NetworkIncludes\TCP_Connexion.h" and default constructor/destructor. That's it.
G++ command:
g++ -o program -I/home/quasiturbine/ServerProject/Network/ /home/quasiturbine/ServerProject/Network/NetworkIncludes/TCP_Connexion.cpp
fatal error:
/home/quasiturbine/ServerProject/Network/NetworkIncludes/TCP_Connexion.cpp:1:43: fatal error: NetworkIncludes\TCP_Connexion.h: No such file or folder
#include "NetworkIncludes\TCP_Connexion.h"
What is wrong and how can I fix it?

The problem is, that you are using backslashes \ when you should be using forward slashes /. Backslashes in include paths are undefined behavior before C++11 and implementation defined afterwards (reference).
So change your include to
#include "NetworkIncludes/TCP_Connexion.h"
and you should be good to go.

Related

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>.

Confusion between including header files and source files in main program

I have heard that we should write the declarations in a header file and the definition in a source file, with both the source and the header having the same name. And then just include the header in the source.
Here is an example myFile.h:
void printer (void);
Here is the implementation of printer in myFile.cpp:
#include "myFile.h"
#include <iostream>
using namespace std;
void printer ()
{
cout<< "I am a printer";
}
Here is my main.cpp:
#include "myFile.h"
int main ()
{
printer();
return 0;
}
Now when I run the program, I get the expected error: undefined reference to printer. But when I see code on github or other projects I see that usually they have included the header file and not the source file. I also tried using the header guards ifndef but still the same error came.
The main program is successfully compiled if:
If i include myFIle.cpp in myFile.h
If i include just myFile.cpp in main
What I the general practice while doing the same?
You should include your myFile.cpp in the linking process:
g++ myFile.cpp main.cpp
The error message undefined reference to printer is actual a linker error, not a compiler error.
Explanation
If you use only g++ main.cpp compiler won't create code from myFile.cpp. He knows that there should be a function with the signature void printer(void), but he doesn't now yet where this function is. He completely ignores this fact, since you can provide pre-compiled object files ("myFile.o") and link those later:
g++ myFile.cpp -c # compile myFile.cpp
g++ main.cpp -c # compile myFile.cpp
g++ myFile.o main.o # link both files together.
-c will tell g++ only to compile the files, but not link them together to an executable. This is done by a linker (g++ will probably call ld in your configuration). The linker will create an executable which contains all needed libraries like libc++ and actual code.
IDE remarks
If you use an IDE make sure that all needed files are included in the project. This includes all header and source files and linkage options for additional libraries.
When yourself define a header file and want to include it, you should enclose it "", such as :
#include "myFile.h"
#include "myFile.h" // would be better.
It seems you forgot the " surrounding the include.
You should use
#include "myFile.h"
or
#include <myFile.h>
the later is rather for system libraries. Both forms differ in the way the search the file.
You find more details on
http://msdn.microsoft.com/en-us/library/36k2cdd4%28v=vs.71%29.aspx

compilation error when including directory containing headers

I have a directory maths which is a library that is comprised solely of header files.
I am trying to compile my program by running the following command in my home directory:
g++ -I ../maths prog1.cpp prog2.cpp test.cpp -o et -lboost_date_time -lgsl -lgslcblas
but I get the following compilation error:
prog1.cpp:4:23: fatal error: maths/Dense: No such file or directory
compilation terminated.
prog2.cpp:6:23: fatal error: maths/Dense: No such file or directory
compilation terminated.
maths is located in the same directory(i.e. my home directory) as the .cpp files and I am running the compilation line from my home as well.
prog1.cpp and prog2.cpp have the following headers
#include<maths/Dense> on lines 4 and 6 respectively, hence I am getting the error.
how do I fix it.
You can either change your include path to -I.. or your includes to #include <Dense>
Wait, if maths is in the same directory as your source files and that is your current directory, you can either change your include path to -I. or your includes to #include "Dense"
maths is located in the same directory(i.e. my home directory) as the .cpp files
Your include path is given as -I ../maths. You need -I ./maths – or simpler, -I maths since maths is a subdirectory of the current directory, not of the parent directory. Right?
Then in your C++ file, use #include <Dense>. If you want to use #include <maths/Dense> you need to adapt the include path. However, using -I. may lead to massive problems1, I strongly advise against this.
Instead, it’s common practice to have an include subdirectory that is included. So your folder structure should preferably look as follows:
./
+ include/
| + maths/
| + Dense
|
+ your_file.cpp
Then use -I include, and in your C++ file, #include <maths/Dense>.
1) Consider what happens if you’ve got a file ./map.cpp from which you generate an executable called ./map. As soon as you use #include <map> anywhere in your code, this will try to include ./map instead of the map standard header.

BOOST_PP_ITERATE() result in "no such file or directory"

I'm learning the boost preprocessor library (because i need to use it), and I wanted to try the file iteration mechanism. I've set up a minimal project with a.cpp and b.hpp. What I'm trying to do is including many time b.hpp via the boost pp :
#include <boost/preprocessor/iteration/iterate.hpp>
#define BOOST_PP_ITERATION_LIMITS (0, 5)
#define BOOST_PP_FILENAME_1 "b.hpp"
#include BOOST_PP_ITERATE()
When I try to compile (with -E to see the preprocessor result) :
g++ -E a.cpp > pp_result
I got this error :
In file included from a.cpp:
/usr/local/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47:37: error: b.hpp: No such file or directory
b.hpp is in the same directory, I can't see what I'm dooing wrong. It seems the g++ searches b.hpp in the same directory as forward1.hpp, but following the boost documentation my code should work (my boost version is 1.44).
Does anybody experienced the same problem ?
Yep, you need to add -I. to the command line in order to make it work. This adds the directory you started gcc in to the include search path, allowing the compiler to find the file b.hpp.

Compilation failing - no #include - boost

I'm trying to compile a third-party library, but g++ is complaining about the following line:
typedef boost::shared_ptr<MessageConsumer> MessageConsumerPtr;
The strange thing is, there is no #include directive in the file - and it is clearly supposed to be this way; there are about 60 files with the same (or very similar) issues. Clearly if there was an #include directive referencing the relevant boost header this would compile cleanly.
My question is: how can I get g++ to somehow automagically find the relevant symbol (in all instances of this issue, it is a namespace that can't be found - usually std:: or boost::) by either automatically processing the relevant header (or some other mechanism).
Thanks.
Edit
My current g++ call looks like:
g++ -fPIC -O3 -DUSING_PCH -D_REENTRANT -I/usr/include/boost -I./ -c MessageInterpreter.cpp -o MessageInterpreter.o
You can use the -include command line option:
g++ -include boost/shared_ptr.hpp ...
From the man page:
-include file
Process file as if "#include "file"" appeared as the first line of
the primary source file. However, the first directory searched for
file is the preprocessor's working directory instead of the
directory containing the main source file. If not found there, it
is searched for in the remainder of the "#include "..."" search
chain as normal.
Create your own wrapper .h file that includes the boost .h and then the broken .h .
Or (very fragile) ensure that you precede every use of the broken .h with boost .h .
Perhaps the third-party library is designed in such a way that you should always include a certain "main" header file in order to get the dependencies right.
Otherwise, you can add #include <boost/shared_ptr.hpp> before including the third-party header file that is giving the error message.