Extract all included files in cpp - c++

I have .cpp file (text).I want to get the list of all files names which are included (#include) to this file.
What is the best way to do it?(Need to implement it in C++)

gcc -M source.cpp
Replace -M with -MM if you don't care about the system includes.

Assuming you have a "find" or "grep", something along these lines:
g++ -E source.cpp | grep '\# 1 '

Related

compile folder of cpp files into GNU's g++ using Powershell commands

I'm looking for a way to pipe a full folders worth of c++ commands into g++ so that I don't have to type 25 file names into the g++ command.
I am using Powershell and thought that I could somehow use a pipeline with Get-childItem -Name -Path *.cpp. That way the whole folder could be compiled at once without the chance of me missing a file or misspelling a filename, and would be modular enough that I could just use Set-Location folderpathbefore pasting it wouldn't have to write a new makefile each time I make a linked program with a medium~large number of linked files.
Is this possible/practical or should I stick to using a makefile?
For example:
I have a elevator simulation 'borrowed' from a how to program book that has 12 header files, 12 classes saved into individual cpp files, and 1 main execution cpp in C:\Users\Noah\Desktop\ripsfromthebook\ch09\elevator.
let's assume that everything necessary for the program to work is present and if you typed in each cpp filename into a g++ command it would compile correctly into a working a.exe file.
They are named:
bell.cpp
bell.h
building.cpp
building.h
button.cpp
button.h
clock.cpp
clock.h
door.cpp
door.h
elevator.cpp
elevator.h
elevatorButton.cpp
elevatorButton.h
ElevatorSimulation.cpp
floor.cpp
floor.h
floorButton.cpp
floorButton.h
light.cpp
light.h
person.cpp
person.h
scheduler.cpp
scheduler.h
It would be great if I could use something similar to Get-ChildItem -Name -Path *.cpp | g++ -g to have all 13 ~.cpp file names be used as an input instead of having to write out this monster {see below} into the powershell commands.
Or using an array of strings to save the childItem output then run g++
$x = Get-ChildItem -name -path *.cpp
g++ -g $x -o main
would be better than this monster.
g++ -g bell.cpp building.cpp button.cpp clock.cpp door.cpp elevator.cpp elevatorButton.cpp ElevatorSimulation.cpp floor.cpp floorButton.cpp light.cpp person.cpp scheduler.cpp
short term solution I'm copy/pasting the output of Get-ChildItem into my makefile.
if your are using Cmakelists to build your library/exe you can use file() as below :
file(GLOB your_src
"relativePath/*.h"
"relativePath/*.cpp")
add_executable(your_exe ${your_src})
but it seems to be advised against
GLOB will generate a list of all files that match the globbing expressions and store it into the variable. Globbing expressions are similar to regular expressions, but much simpler. If RELATIVE flag is specified for an expression, the results will be returned as a relative path to the given path. (We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.)
source: https://cmake.org/cmake/help/v3.0/command/file.html#file

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.

GCC Compiler: Compile Multiple files with different output file names

Is it possible to compile multiple files and save the output for each files with different name ? I had only copied .cpp files from old computer to my new one.Those programs are all error free and are tested.So now i have to compile each files to get the output file.There are about 310 programs so it is really hard to compile each file separately.I usually save output file with the same file name without any extension.Is there any way to compile all files in the directory and save each files output separately. I'm looking forward for a command like this
gcc *.cpp -o *
If there are files,
filename1.cpp
filename2.cpp etc.
I want to get the output files like this :
filename1
filename2 etc.
EDIT :
Is there any way to save the timestamp of .cpp file to the output file .??
If each file should have it's own executable and they're all in the same directory you can do this:
for i in *.cpp; do g++ $i -o `basename $i .cpp`; done
To add the timestamp:
for i in *.cpp; do g++ $i -std=c++11 -o `basename $i .cpp`-`date +%Y%m%d -r $i`; done
This will produce the date in YYYYMMDD format after the filename and hyphen
To change modification date:
for i in *.cpp; do g++ $i -std=c++11 -o `basename $i .cpp`; touch -t `date +%Y%m%d%H%M -r $i` `basename $i .cpp`; done
You would do this with the "make" program, and a suitable makefile. That uses rules (some predefined), for transforming ".cpp" files into ".o" and executables. A quick check shows me that GNU make does have a default rule for .cpp to .o (long ago, it did not).
The GCC compiler is (internally) compiling one file at a time (but also has Link Time Optimization).
You want to use a builder like GNU make; adapt this example (or that one) to your needs.

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

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.

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.