rezon rezon - 2 months ago 9
C Question

warning: implicit declaration of function - order of includes matter?

I have the following sample code snippet of code from a main.c which calls 3 functions, with 3 headers - which is giving me warnings for a reason unknown:



#include "header1.h"
#include "header2.h"
#include "header3.h"

int main()
{
function1(); // this is from header1
function2(); // this is from header2
function3(); // this is from header 3
}





Basically, after using gcc, the functions 2 & 3 will produce the warning. However, after rearranging the code to something like this:



#include "header3.h"
#include "header1.h"
#include "header2.h"


int main()
{
function1(); // this is from header1
function2(); // this is from header2
function3(); // this is from header 3
}





It will then give me the warning that functions 1 & 2 are implicit. What am I doing wrong here?

Answer

your include file 1 should look like this:

#ifndef __REZON_FUNCTIONS1__ 
#define __REZON_FUNCTIONS1__ 

#endif

The other two files should be similar with the macro name changed accordingly