我已经将我的C源代码拆分成两个文件:分别是main.c
和secondary.c
.
/* 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);
}
我使用以下命令来编译它们:
$gcc -Wall main.c secondary.c
但在这种情况下,我收到以下警告:
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();
| ^~~~~~~~~~~
然后我决定在main.c
中加入secondary.c
.为此,我在main.c
中键入#include "secondary.c"
.
在本例中,我得到以下错误:
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
我该怎么做才能摆脱-Wimplicit-function-declaration
人呢?