I often split the program into small modules, each with its own header and implementation. In general, the level of abstraction used allows the implementation to be completely changed without breaking the rest of the module-dependent code.
In some benchmarks that I ran using callgrind, implementing the functions of a commonly used module in the header also produced considerable gains in performance. In these cases I did the following:
modulo.h
#include <stdlib.h> //cabecalhos necessários
#ifndef MODULO_IMPLEMENTACAO
inline int funcao1(int x, int y)
{
//código
}
inline int funcao2(int x, int y, double z)
{
//código
}
#endif
modulo.c
#define MODULO_IMPLEMENTACAO
#include "modulo.h"
extern inline int funcao1(int x, int y)
{
//código
}
extern inline int funcao2(int x, int y, double z)
{
//código
}
If I understand correctly, the problems with this approach are that you have to recompile all the files that depend on the module in question should the implementation change, take longer to compile, and create functions that would not have previously existed - which were declared static
and implemented inline by the compiler.
Are there any other disadvantages in this practice? When to implement the functions in the header?