The problem here is that you are including commands.c
in commands.h
before the function prototype. Therefore, the C pre-processor inserts the content of commands.c
into commands.h
before the function prototype. commands.c
contains the function definition. As a result, the function definition ends up before than the function declaration causing the error.
The content of commands.h
after the pre-processor phase looks like this:
#ifndef COMMANDS_H_
#define COMMANDS_H_
// function definition
void f123(){
}
// function declaration
void f123();
#endif /* COMMANDS_H_ */
This is an error because you can't declare a function after its definition in C. If you swapped #include "commands.c"
and the function declaration the error shouldn't happen because, now, the function prototype comes before the function declaration.
However, including a .c
file is a bad practice and should be avoided. A better solution for this problem would be to include commands.h
in commands.c
and link the compiled version of command to the main file. For example:
commands.h
#ifndef COMMANDS_H_
#define COMMANDS_H_
void f123(); // function declaration
#endif
commands.c
#include "commands.h"
void f123(){} // function definition