Are there any problems with defining a single-source “module” in C using conditional compilation to separate the “header” from “source”

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/286898

  •  09-10-2020
  •  | 
  •  

Domanda

I'm writing a project that I want to keep small in the sense of being very densely-coded and a single source file.

But it's growing large enough that navigating the file is becoming tiresome, so I want to break it up into several parts. But I think I can keep the number of files small by not having a separate header file but instead using preprocessor directives to designate sections:

#ifdef EXPORT
#define blah Blah Blah Blah
void prototype(void);
#else /* IMPLEMENTATION */

void prototype(void){
     //blah
}

#endif /* IMPLEMENTATION */

and to include the "header":

#define EXPORT
#include "module.c"
#undef EXPORT

Are there any drawbacks to this that I'm just not seeing? Aside from the fact that I've never heard of anybody doing this?

È stato utile?

Soluzione

Compilation dependencies are from file to file. By separating the declarations into a separate header file, you are free to change the source without causing a recompilation of other code that depends on the header.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top