菜鸟笔记
提升您的技术认知

extern的作用

extern的作用是使一个文件的中变量可以被其他文件使用举个例子

test.h

#pragma once
extern int buf; //在头文件中声明cpp文件中定义

test.cpp

#include"test.h"
int buf = 5;

main.cpp

#include<iostream>
#include"test.h"
using namespace std;
int main()
{
    cout << buf;
    return 0;
}

最后输出结果为5