#ifndef MY_H_INCLUDED #define MY_H_INCLUDED extern int a ; extern const int b; #endif // MY_H_INCLUDED
#include "my.h" int a = 100 ; extern const int b = 200 ;
#include <iostream> #include "my.h" int main() { std::cout<<a<<std::endl; std::cout<<b<<std::endl; return 0; }
结论:对于const类型常量,需要申明和定义都加上extern。而,普通全局变量,只需要在头文件申明时加extern
时间: 2024-08-23 21:26:22