A quick way to determine what is needed includes C ++

On Linux, what is a quick way to determine which necessary #include statements I need for a C ++ project?

I mean, let's say someone gives you a snippet from the Internet but cannot provide the necessary #include instructions. Is it possible, when you can run a Linux command line command or a compiler and determine which functions or classes are missing, and, as a bonus, determine on your hard drive where I could have these things in the header file.

+4
source share
5 answers

Basically, you need some kind of analyzer to analyze your sources and headers and build a full dependency chart, which it eventually splashes out so you can read and process it further.

I will follow John’s advice on g ++ and Clang for this purpose, but I highly doubt that they got what they needed.

What you can actually do, at least with g ++, prints a graph for existing ones. Use the -H option to print the tree or -M to get a list.

I also refer to this related topic: #include dependency tracking tool

Not quite what you want, but the tools mentioned there can be useful.

+4
source

I think the Clang tool "include-what-you-use" is what you want.

+2
source

If by necessity you mean minimal (that is, if A includes B and B includes C, then A does not need to include C) I do not know a quick way.

However, one good approach is that each cpp file should include its own header file (after any precompiled headers). This ensures that each header file contains (directly or indirectly) all header files needed to identify the characters used in the header.

In addition, a design of reasonable size should be designed in such layers that Layer A knows about / depends on level B, which depends on level C, etc., but the lower layers never include higher levels (i.e. C never include anything from layer A)

In this case, the cpp or hpp included in each should be in Layer order (A, B, C). If you do this, it’s pretty easy to check if you can exclude any of the headers of the C layer (temporarily comment on them), since one of the options included in it has already included them. This happens quite a lot and can significantly reduce the number of #includes in each file.

Having said all this, this is a much less important issue than before, because compilers are smarter. The combination of one-time and pre-compiled #pragma headers can reduce build time without requiring you to spend a lot of time optimizing.

0
source

The best way to find out what to find undefined identifiers in a program is to simply try to compile it. Depending on what you use for the compiler, you can simply connect the output of GCC or Clang to grep, looking for phrases such as "undeclared identifier".

Regarding determining where the characters are defined, I would recommend looking at Ctags as a starting point to analyze your system headers (best managed with the Makefile) and using the resulting tag table to look for anything grep catches from GCC.

0
source

The fastest way ... this is not how you should think about it.

fooobar.com/questions/1168512 / ...

I wrote a beautiful (I'm very proud of: P) answer there, talking about how links (with templates) work and proves that it works and what it is, understand it.

The purpose of the #include directives is to create a "translation unit" where each character is declared (even if it is not defined), there is an example in my answer where I just copy and paste the prototype into the code file and not use include.

You do not have to worry about the “fastest” way if you use something called the “Header guard” (they are mentioned briefly below, but this is not detailed enough), they go as follows:

 #ifndef __WHATEVER_H #define __WHATEVER_H /*Your code here*/ #endif 

So now you can enable "whatever.h" as many times as you want. for the first time, __WHATEVER_H will be defined in the translation module, so the next file that includes it (although many of them include deep from the compiled file) will be empty. since everything between #ifndef and #endif will be missing.

Hope this helps.

Also, if you have unnecessary inputs, use -Wextra and -Wall, GCC will tell you about unused functions, typedef, etc. you can use pragma error push and pop things to control this. For example, wxWidget header files can contain many unused things, so you push warnings onto the stack, delete unused warning flags, include a file, put up a warning stack (turn them back), less you get thousands of warning lines.

-one
source

All Articles