I've splitted up my C source code into 2 files: main.c and secondary.c respectively.
/* main.c */
#include <stdlib.h>
//#include "secondary.c"
int main ()
{
extern int my_var;
my_var = 123;
print_value ();
exit (0);
}
/* secondary.c */
#include <stdio.h>
int my_var;
void print_value ()
{
printf("my_var = %d\n", my_var);
}
I use the following command to compile them:
$gcc -Wall main.c secondary.c
But in this case I receive the following warning:
snowgold@iskhakov-r:~/clang$ gcc -Wall main.c secondary.c
main.c: In function ‘main’:
main.c:9:9: warning: implicit declaration of function ‘print_value’ [-Wimplicit-function-declaration]
9 | print_value();
| ^~~~~~~~~~~
Then I decide to include secondary.c in main.c. To do so, I type #include "secondary.c" in main.c.
In this case, I get the following error:
snowgold@iskhakov-r:~/clang$ gcc -Wall main.c secondary.c
/usr/bin/ld: /tmp/ccKKRtFc.o:(.bss+0x0): multiple definition of `my_var'; /tmp/ccx4HrNG.o:(.bss+0x0): first defined here/usr/bin/ld: /tmp/ccKKRtFc.o: in function `print_value':
secondary.c:(.text+0x0): multiple definition of `print_value'; /tmp/ccx4HrNG.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
What do I do to get rid of -Wimplicit-function-declaration?
According to the first compiler message the compiler does not know how the function
print_value()used in the modulemain.cis declared when it compiles the module.Create a header with the function declaration (prototype) like
and include the header in the both modules.
As for the second message then after including
secondary.cinmain.cyou have two definitions of the same function in the both modulesmain.candsecondary.c. Do not include c midules in each other.Pay attention to that according to the C Standard the function
mainwithout parameters shall be declared likeAlso this statement
is redundant. Remove it.
Also bear in mind that according to the C23 Standard you may not use functions without function prototypes as it was allowed before the C23 Standard.