Comments on #include a safe way to see if it is not needed?

I like when my files are clean, so I prefer to take out what I don't need. Recently, I just comment on what it turns on and sees if it compiles without warning ( -Wall -Wextra -pedantic , minus a few very specific ones). I believe that if it compiles without warning, I donโ€™t need it.

Is this a safe way to check if inclusion is required or can it introduce UB or other problems? Are there any specific warnings that I need to be sure I can catch potential problems?

<sub> nb I actually use Objective-C and clang, so everything that is especially important to them is appreciated, but given the flexibility of Objective-C, I think that if there are any problems, this will be a common thing C. Of course Any problems in C will affect Objective C.

+6
source share
4 answers

I guess, yes.

An exception would be if the two headers interact in some hidden way. Let's say if you:

  • include two different headers that define the same character in different ways,
  • both definitions are syntactically valid and well typed,
  • but one definition is good, the other is at run time.

I hope your header files are not structured like this. This is somewhat unlikely, although unthinkable.

I would be comfortable doing this if I had good (single) tests.

+2
source

Usually just commenting on the inclusion of the header is safe, which means: if the header is needed, then there will be compiler errors when deleting it and (usually) if the header is not needed, the code will still compile.

This should not be done without checking the header to see what it adds, since there is a (not quite typical) possibility that the header provides only an optional #define (or # undef) that will change, but not interrupt, the way the program is compiled.

The only way to make sure that you create your code without a header (if it can build in the first place) and run the proper testing mode so that its behavior does not change.

+1
source

In general, no. Easy to enter silent changes.

Suppose header.h defines some macros, such as

 #define WITH_FEATURE_FOO 

C file including header.h validates macro

 #ifdef WITH_FEATURE_FOO do_this(); #else do_that(); #endif 

Your files are compiled and included with all warnings with or without header.h , but the result behaves differently. The only way to get the final answer is to analyze which identifiers the header defines / declares and sees if at least one of them appears in the pre-processed C file.

One tool that does this is Gimpel's FlexeLint. I am not paid for this, although they should :-) If you want to avoid getting around big bucks, the approach I took is to compile the C file into an object file with and without a header, if both successfully verify identical object files. If they are the same, you do not need a header (but see our include include directives, enclosed in #ifdef , which are activated by the -DWITH_FEATURE_FOO option).

+1
source

No. In addition to the reasons already mentioned in other answers, it is possible that a heading is needed and another heading includes it indirectly. If you remove #include , you will not see an error, but there may be errors on other platforms.

+1
source

All Articles