Get #include headers after inclusion

Let's say that the two header A.hpp and B.hpp and A.hpp enter the second file through the #include "A.hpp" . Now that you are distributing the header files, I need another parameter that includes one header file, A_B.hpp , which contains exactly the same code as the contents of A.hpp inserted in its #include statement.

At the moment, I manually copy the contents of the content, since there are fewer releases, and the file size is not so much, but is there a way to do something like this? Something like command B.hpp > A_B.hpp

Another thing to keep in mind, I don’t want other preprocessor things like macro extensions, special OS detection macros like

 #if defined(__unix__) || defined(__unix) || defined(__linux__) #define OS_LINUX #elif defined(WIN32) || defined(_WIN32) || defined(_WIN64) #define OS_WIN #elif defined(__APPLE__) || defined(__MACH__) #define OS_MAC #else #error Unknown Platform #endif 

to get an estimate, since they must be present in the final file. Any other way to do such taks is also welcome.

+5
source share
2 answers

For this you need to write your own script. What you are asking for is much less than gcc -E (the preprocess of everything). Also, this is less than what cpp -fdirectives-only does because you don't want to recurs. You really have requirements for specific projects, and you will need to write code for the project for it.

+1
source

You may be looking for the unifdef utility.

Say I have two header files A.hpp and B.hpp, and A.hpp is included in the second file using the #include "A.hpp" instruction. Now, when distributing header files, I need another option, which includes one header file, A_B.hpp, which contains exactly the same code as the contents of A.hpp inserted above its #include statement.

Actually, you do not say that A.hpp and B.hpp (and details are important). They probably include some system headers (for example, <stdio.h> from the C standard library or <new> or <vector> headers from the C ++ standard library). Then distributing their pre-processed form (output g++ -C -E b.hpp ...) is practically useless, because this form then contains some system / compiler-specific declarations / definitions that are not portable to another compiler (in practice, <map> from GCC 5 is probably not compatible with <map> from GCC 6).

The whole point of standard libraries is that they normalize the name of the headers (and their public interfaces), such as <vector> or <stdio.h> , but not the details of their contents (and private implementations). And compilation options (e.g. -pthread or -std=gnu++11 or -O ...) can influence the pre-processed form (especially through common predefined macros ).

You can use the -H version of the preprocessor before g++ to find out what is included (or even play tricks with various -M ...).

You might want to distribute the source code pooling (for example, SQLite does ), but this requires some careful development effort.

You can use some external preprocessor, such as m4 or GPP .

(but it is project specific and much less simple than you think, precisely because of the role of standard headers in your projects)

+1
source

Source: https://habr.com/ru/post/1265342/


All Articles