在fedora上编译C程序时出现警告如何修复

How to fix warning occured during compiling of C program on fedora

我正在尝试学习如何将一个 c 程序拆分为多个文件,但在编译期间它会抛出警告,如下所示:

$ gcc ./p1.c ./p2.c -o ./p1
./p2.c:2:1: warning: data definition has no type or storage class
    2 | b = 6;
      | ^
./p2.c:2:1: warning: type defaults to ‘int’ in declaration of ‘b’ [-Wimplicit-int]

其中, p1.c

#include <stdio.h>
#include "p2.h"
int a = 5;

int main(void) {
    printf("%d\n", a);
    printf("%d\n", b);
    printf("%d\n", square(b));
    return 0;
}

p2.c

#include "./p2.h"
b = 6;
int square(int x) {
    return x * x;
}

p2.h

#ifndef P2_H
#define P2_H

extern int b;
int square(int);

#endif

所有这些文件都存在于同一目录中,但我仍然收到警告,在互联网上尝试多次搜索后我找不到修复方法。

提前致谢。

p2.c应该有全局变量的正常定义:

#include "./p2.h"
int b = 6;
int square(int x) {
    return x * x;
}