Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it considered good practice to have conditionals in public header files?

My C library has some optional features, and using automake, the user can turn them on and off by providing flags to configure.

If a feature is turned off, that function will not be compiled.

However, my question is, should I also remove the function prototype from the public headers in that case?

It seems like not a good idea to have function prototypes for functions that are not compiled, but it also seems to me not a good idea to have different public headers installed depending on the library configuration. (Similar to how it's bad practice to install config.h in the public headers directory.)

What's the best approach for public headers when it comes to optional features? If a user tries to use a disabled feature, should the error come at compile time, or link time? There must be a standard practise for this situation. (I prefer to comply with GNU coding standards if there are multiple ideas, but I don't know of the GNU standard on this issue.)

like image 270
Steve Avatar asked Mar 09 '12 18:03

Steve


People also ask

Should header files include Stdio?

stdio. h is a header file which has the necessary information to include the input/output related functions in our program. Example printf, scanf etc. If we want to use printf or scanf function in our program, we should include the stdio.

What should be included in a header file?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .

Why header files inclusion is must?

Including Multiple Header Files: You can use various header files in a program. When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this error, conditional preprocessor directives are used.

Should you include header files in other header files?

A header file should be included only when a forward declaration would not do the job. The header file should be so designed that the order of header file inclusion is not important.


3 Answers

Definetely don't only exclude the implementation from the compilation, but the whole function.

//feature.h
#ifndef EXCLUDE_FEATURE
   void foo();
#endif

//feature.c
#include "feature.h"
#ifndef EXCLUDE_FEATURE
void foo()
{
}
#endif

This way, you'll get a compiler, not linker error, if you try to use foo with the feature excluded. You want to signal an error as soon as possible, linker errors in general transmit less about the intention of the developer.

Microsoft does this (conditionals in header files) for MFC and it works quite nicely. (of course that's C++, but the principle stands.

like image 170
Luchian Grigore Avatar answered Oct 04 '22 11:10

Luchian Grigore


I think there are 2 valid approaches to this problem

  • Have a single header file which uses #ifdef to remove functions which aren't supported in certain configurations
  • Have multiple header files with no #ifdef each of which is specific to a configuration

It seems like a really bad practice to leave functions which aren't present in the lib in the header file for a given configuration. It will take what should be a compile time error and move it to a linker one.

like image 41
JaredPar Avatar answered Oct 04 '22 11:10

JaredPar


I observed the following approach in some projects: Generate the header file from a template one.

The file generation is based on the configuration flags.

This approach seemed to me more clean than using never ending conditionals definitions in the header... to me it seems much more clean.

Disadvantage: It may be a support burden (for the script).

like image 22
EdwardH Avatar answered Oct 04 '22 11:10

EdwardH