c语言extern详解

发布时间:2023-05-21

在C语言中,extern关键字用于声明一个在其他文件中已定义的全局变量、全局函数或类等符号,在本文中将从多个方面对C语言extern进行详细的阐述。

一、extern关键字

  1. 关键字含义 在C语言中,extern关键字用于说明某个变量或函数是在其他文件中定义的,因此在当前文件中需要引用。extern可以用于变量和函数,其作用是在使用本文件中未定义的全局变量或函数时,告诉编译器在其他文件中寻找其定义。
  2. 变量用法
    extern int a; // 声明全局变量a
    int main() {
        printf("a = %d\n", a); // 使用变量a
        return 0;
    }
    
    变量a在当前文件中未定义,因此需要用extern声明。在使用变量a时,编译器会在其他文件中寻找其定义。
  3. 函数用法
    extern void hello(); // 声明函数hello
    int main() {
        hello(); // 调用函数hello
        return 0;
    }
    
    函数hello在当前文件中未定义,因此需要用extern声明。在调用函数hello时,编译器会在其他文件中寻找其定义。

二、extern与全局变量

  1. 变量定义
    // a.c文件
    int a = 10; // 定义全局变量a
    
    // b.c文件
    extern int a; // 引用全局变量a
    int main() {
        printf("a = %d\n", a); // 输出全局变量a的值
        return 0;
    }
    
    在a.c文件中定义了全局变量a,在b.c文件中用extern引用全局变量a。在编译b.c时,编译器会在其他文件中寻找全局变量a的定义。
  2. 变量声明
    // a.c文件
    extern int a; // 声明全局变量a
    int func() {
        return a;
    }
    
    // b.c文件
    int a = 10; // 定义全局变量a
    int main() {
        printf("a = %d\n", func()); // 输出全局变量a的值
        return 0;
    }
    
    在a.c文件中用extern声明全局变量a,在b.c文件中定义全局变量a。在a.c的func函数中引用了全局变量a,在调用func时,编译器会在其他文件中寻找全局变量a的定义。

三、extern与函数

  1. 函数定义
    // a.c文件
    void hello() { // 定义函数hello
        printf("Hello, world!\n");
    }
    
    // b.c文件
    extern void hello(); // 引用函数hello
    int main() {
        hello(); // 调用函数hello
        return 0;
    }
    
    在a.c文件中定义了函数hello,在b.c文件中用extern引用函数hello。在编译b.c时,编译器会在其他文件中寻找函数hello的定义。
  2. 函数声明
    // a.c文件
    void hello(); // 声明函数hello
    void world() {
        hello(); // 调用函数hello
    }
    
    // b.c文件
    void hello() { // 定义函数hello
        printf("Hello, world!\n");
    }
    int main() {
        world(); // 调用函数world
        return 0;
    }
    
    在a.c文件中用extern声明函数hello,在a.c的world函数中调用了函数hello,在b.c文件中定义函数hello。在调用world函数时,编译器会在其他文件中寻找函数hello的定义。

四、extern与头文件

  1. 头文件定义
    // add.h头文件
    int add(int a, int b); // 函数声明
    
    // add.c文件
    #include "add.h" // 包含头文件
    int add(int a, int b) { // 定义函数
        return a + b;
    }
    
    // main.c文件
    #include "add.h" // 包含头文件
    int main() {
        int sum = add(1, 2); // 调用函数
        printf("sum = %d\n", sum);
        return 0;
    }
    
    在add.h头文件中定义了函数add的声明,在add.c文件中定义了函数add的实现,在main.c文件中包含了add.h头文件并调用了函数add。
  2. 多文件定义
    // a.c文件
    extern int x; // 引用全局变量x
    void hello(); // 声明函数hello
    
    // b.c文件
    int x = 123; // 定义全局变量x
    void hello() { // 定义函数hello
        printf("Hello, world!\n");
    }
    
    // main.c文件
    #include <stdio.h>
    extern int x; // 引用全局变量x
    void hello(); // 声明函数hello
    int main() {
        printf("x = %d\n", x);
        hello();
        return 0;
    }
    
    在a.c文件中用extern引用全局变量x和函数hello,在b.c文件中定义全局变量x和函数hello,在main.c中用extern引用全局变量x和函数hello并调用函数hello。在编译时,需要将a.c、b.c和main.c都编译链接在一起。

五、extern使用注意事项

  1. 变量定义
    // a.c文件
    extern int a; // 引用全局变量a
    
    // b.c文件
    int a = 10; // 定义全局变量a
    
    在a.c中用extern引用全局变量a,在b.c中定义全局变量a。如果在a.c中使用变量a,将会出现链接错误。
  2. 函数定义
    // a.c文件
    extern void hello(); // 引用函数hello
    
    // b.c文件
    void hello() { // 定义函数hello
        printf("Hello, world!\n");
    }
    
    在a.c中用extern引用函数hello,在b.c中定义函数hello。如果在a.c中调用函数hello,将会出现链接错误。

六、总结

通过以上的阐述和示例,我们了解到了C语言extern关键字的作用和使用方式,以及与全局变量和函数的配合使用。在多文件开发中,extern是一个重要的关键字,需要注意使用细节。