How to preprocess individual source file with several #include files spread across in large project? - c++

For getting a preprocessed output, following command is used:
g++ -E a.cpp > temp.cpp
If a.cpp has #include files not in the same folder, then we use:
g++ -I <directory> -E a.cpp > temp.cpp
Now, there are several .cpp file for which I want a preprocessed output.
Also a huge #include dependency for every .cpp file and the header file is spread in several different subdirectories; so following option is very cumbersome:
g++ -I <dir1> -I <dir2> -I <dir3> ... -E a.cpp > temp.cpp
Is there any simpler way (preferably without the use of Makefile) ?
(Assume that dir1, dir2, ... are under a single directory)

You need the same set of -I for preprocessing and for compiling.
When you actually compile the code, you surely use some set of -I parameters (probably generated by a make file).
So just use the same (or compile, and copy&paste the -I parameters).
The only alternative I see is restructuring your header directories to be in a common directory (with sub-directories, using #include <subdir/header.h>).
But this is much more work (though it may be worth the effort).

One way can be that you give the TOP directory in which all the subdirectory resides. This way you wont have to give many -I. But this will slowdown it.

Related

G++ Add include and linking path

My Problem
I read somewhere that to add include paths to g++ you need to use the -I option:
g++ -I /some/directory and_then_my_files.cpp
However, I'm not sure if this is actually what I need. It's currently not working for me, and I couldn't find anything else close to what I have.
My Setup
I've got a directory with all my current project code, and in it a subdirectory classes, that contains various .h and .cpp files containing classes that may or may not be used when compiling my main files.
Since g++ ./classes/*.cpp main.cpp takes a long time (large number of files in classes directory), I'm looking for an alternative that only compiles and links the files that are included in the main file.
Main file:
#include "classes/a.h"
#include "classes/b.h"
// ... my code
And as you can imagine, g++ complains about undefined references to classes A and B, unless I add ./classes/*.cpp to the build command.
What I want to achieve
So -I and -L did not work, and adding the whole directory to the build command results in a ridiculously long build time - I'm talking 3-5 minutes, which really slows down my development speed.
Is there any way to only build/link the included classes from my classes directory, such as only classes A and B from the example I gave above?
Small Recap of what I've already tried
g++ -I ./classes main.cpp -o main
g++ -L ./classes main.cpp -o main (probably stupid but I tried it anyway)
g++ ./classes/*.cpp main.cpp -o main (what I currently have to resort to)
Thanks in advance :)
A temporary workaround
export CPLUS_INCLUDE_PATH=/usr/local/include

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.

Include one header file in each source file

Say you have 100s of source files (.c or .cpp) files, and you want to include some definitions, function/variable declarations in each of them. Normally in C/C++, you use header files for that purpose. But in this case you need to put #include "header.h" in each source file.
Now my question is, is there a way to include one header for all the files without putting #include "header.h" in each of the file, because it will be very tiresome to write #include "header.h" for 100s of source files.
You can use the -include flag for clang or GCC. 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.
If multiple -include options are given, the files are included in the order they appear on the command line.
Example:
clang -include header.h -c file1.c
clang -include header.h -c file2.c
clang -include header.h -c file3.c
clang -o app file1.o file2.o file3.o
MSVC has the /FI flag, which is similar.
You could solve this problem using a unix pipe
find ./ -name "*.c" -or -name "*.cpp" | xargs -n 1 sed -i '1 i #include <my_header.h>'
You can't do that, although you could write a script for you to do it. A script that takes each files, and writes #include "header.h" at top. Edit: -include in gcc does this.
However, what you need is achievable in a different way through the compiler options. In gcc, with -D.
Let's say, you want the define DEBUG_LEVEL to 2 in all your source files. You can simply do this by invoking gcc like this:
gcc -DDEBUG_LEVEL=2
Note that in this case, you would need to rebuild all your project (which would have been done anyway if you had changed this definition in 1 header file to which ALL the source files depend on)
Header files are not definitions - they are declarations.
You put as few in as possible - saves the compiler work and also inter-dependencies.
You can even reduce the number further by using forward declarations in those header files.
If you are clever you can get you IDE to help you out with filling in the gaps instead of hurting your fingers.

Using G++ to compile multiple .cpp and .h files

I've just inherited some C++ code that was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .h files that contain classes and their function definitions.
Until now the program was compiled using the command g++ main.cpp. Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the g++ main.cpp command?
list all the other cpp files after main.cpp.
ie
g++ main.cpp other.cpp etc.cpp
and so on.
Or you can compile them all individually. You then link all the resulting ".o" files together.
To compile separately without linking you need to add -c option:
g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out
Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the "g++ main.cpp" command?
Compiling several files at once is a poor choice if you are going to put that into the Makefile.
Normally in a Makefile (for GNU/Make), it should suffice to write that:
# "all" is the name of the default target, running "make" without params would use it
all: executable1
# for C++, replace CC (c compiler) with CXX (c++ compiler) which is used as default linker
CC=$(CXX)
# tell which files should be used, .cpp -> .o make would do automatically
executable1: file1.o file2.o
That way make would be properly recompiling only what needs to be recompiled. One can also add few tweaks to generate the header file dependencies - so that make would also properly rebuild what's need to be rebuilt due to the header file changes.
.h files will nothing to do with compiling ... you only care about cpp files... so type g++ filename1.cpp filename2.cpp main.cpp -o myprogram
means you are compiling each cpp files and then linked them together into myprgram.
then run your program ./myprogram
I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.
Let's say you have 5 cpp files, all you have to do is use the * instead of typing each cpp files name E.g g++ -c *.cpp -o myprogram.
This will generate "myprogram"
run the program ./myprogram
that's all!!
The reason I'm using * is that what if you have 30 cpp files would you type all of them? or just use the * sign and save time :)
p.s Use this method only if you don't care about makefile.
You can still use g++ directly if you want:
g++ f1.cpp f2.cpp main.cpp
where f1.cpp and f2.cpp are the files with the functions in them. For details of how to use make to do the build, see the excellent GNU make documentation.
As rebenvp said I used:
g++ *.cpp -o output
And then do this for output:
./output
But a better solution is to use make file. Read here to know more about make files.
Also make sure that you have added the required .h files in the .cpp files.
You can use several g++ commands and then link, but the easiest is to use a traditional Makefile or some other build system: like Scons (which are often easier to set up than Makefiles).
If you want to use #include <myheader.hpp> inside your cpp files you can use:
g++ *.cpp -I. -o out
I used to use a custom Makefile that compiled all the files in current directory, but I had to copy it in every directory I needed it, everytime.
So I created my own tool - Universal Compiler which made the process much easier when compile many files.
You can do that using a single command assuming all the needed .cpp and .h files are in the same folder.
g++ *.cpp *.h -Wall && ./a.out
It will compile and execute at the same time.
when using compiler in the command line, you should take of the following:
you need not compile a header file, since header file gets substituted in the script where include directive is used.
you will require to compile and link the implementation and the script file.
for example let cow.h be header file and cow.cpp be implementation file and cow.cc(c++ files can have extension .cpp, .cc, .cxx, .C, .CPP, .cp) be script file.
Since gcc compiler notation for c++ file is g++, we can compile and link the files using
$g++ -g -Wall cow.cpp cow.cc -o cow.out
options '-g' and '-Wall' are for debugging info and getting warning for errors. Here cow.out is the name of the executable binary file that we can execute to run the program. it is always good to name your executable file otherwise name will be automatically given which might be confusing at times.
you can also do the same by using makefiles, makefiles will detect, compile and link automatically the specified files.
There are great resources for compilation using command line
enter link description here
~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_func.cpp coordin.h
~/In_ProjectDirectory $ ./a.out
... Worked!!
Using Linux Mint with Geany IDE
When I saved each file to the same directory, one file was not saved correctly within the directory; the coordin.h file. So, rechecked and it was saved there as coordin.h, and not incorrectly as -> coordin.h.gch. The little stuff.
Arg!!

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.