I have encountered an issue in C that I am unable to recover, being "redefinition of [function]", and when I checked for a redefinition, I don't see any.
this always occur when the files are written like so:
file2.c
static int sampleFunction(){
return 0;
}
file1.c
#include "file2.c"
static int anotherSampleFunction(){
return sampleFunction()*2;
}
main.c
#include "file1.c"
#include "file2.c"
int main(){
return sampleFunction()+anotherSampleFunction();
}
upon compiling, a
"redefinition of sampleFunction"
will occur in file2.c, even though the function wasn't redefined.
please note that the code is sample code: in reality, the source code is much more complicated, so I tried to simplify.
is there a way to fix this issue? thanks.
because
includewill replace the code of file name that you specify.So in file1.c will be
when come to
main.cit's will definedsampleFunction()twice as you include.So, in
main.cyou have to include onlyfile1.c.but in practical, You should use header files instead of include
.cfiles.