当前位置:网站首页>C language keyword extern
C language keyword extern
2022-07-26 07:30:00 【Kangnamiao】
One . Preface
Recently I wrote a code , Always appear when compiling :multiple definition, It's so annoying to think of it , Then enumerate the possible situations , To sum up .
Two . Code
Compile environment :gcc version 9.4.0 (Ubuntu 9.4.0-1Ubuntu1~20.04)
- a.c
#include <stdio.h>
#include "a.h"
#include "b.h"
int add()
{
printf("%d\n", ++aim);
}
int main()
{
dec();
add();
dec();
return 0;
}
- a.h
#ifndef _A_H
#define _A_H
int add();
#endif
- b.c
#include <stdio.h>
#include "b.h"
int dec()
{
printf("%d\n", --aim);
}
- b.h
#ifndef _B_H
#define _B_H
int dec();
#endif
- makefile
TARGET:=./out
obj-y:=a.c b.c
OBJS=$(patsubst %.c, %.o, $(obj-y))
all:clean $(TARGET)
@./out
$(TARGET):$(OBJS)
@gcc -o $(TARGET) $(OBJS)
%.o:%.c
@gcc $(CFLAGS) -c $<
clean:
@rm -rf $(OBJS) $(TARGET)
3、 ... and . Permutation and combination
hold int aim = 1 and extern int aim Put it under different files 4×3=12 In this case , Then let's try one by one :
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | extern | × | × | In function 'dec': 'aim' undeclared |
| aim=1 | × | extern | × | Output 0 1 0 You can use √ |
| aim=1 | × | × | extern | Output 0 1 0 You can use √ |
| extern | aim=1 | × | × | In function 'dec': 'aim' undeclared |
| × | aim=1 | extern | × | Output 0 1 0 You can use √ |
| × | aim=1 | × | extern | Output 0 1 0 You can use √ |
| extern | × | aim=1 | × | Output 0 1 0 You can use √ |
| × | extern | aim=1 | × | Output 0 1 0 You can use √ |
| × | × | aim=1 | extern | Output 0 1 0 You can use √ |
| extern | × | × | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | extern | × | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | × | extern | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
Define only aim=1
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | × | × | × | In function 'dec': 'aim' undeclared |
| × | aim=1 | × | × | In function 'dec': 'aim' undeclared |
| × | × | aim=1 | × | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | × | × | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
stay .h Statement int aim, stay .c Instantiation int aim = 1
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | aim; | × | × | In function 'dec': 'aim' undeclared |
| aim=1 | × | aim=1 | × | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| aim=1 | × | × | aim; | Output 0 1 0 You can use √ |
| × | aim; | aim=1 | × | Output 0 1 0 You can use √ |
| × | aim; | × | aim; | Output -1 0 -1 Also available |
Ideal standardized writing :
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | aim; | × | aim; | Output 0 1 0 You can use √ |
| aim=1 | aim; | × | extern aim; | Output 0 1 0 You can use √ |
| × | aim; | aim=1 | aim; | Output 0 1 0 You can use √ |
| × | extern aim; | aim=1 | aim; | Output 0 1 0 You can use √ |
Another special :
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| × | int aim; | static int aim=1 | × | 0 1 -1( there aim Just two variables static The overall role of ) |
| × | int aim; | × | static int aim=1 | static declaration of 'aim' follows non-static declaration |
| × | static int aim; | × | static int aim=1 | 0 2 -1 |
| static int aim; | × | × | static int aim=1 | 0 2 -1 |
Four . Disgusting situation
Considering that header files often reference each other in projects , It will be b.c Add a line of code #include "a.h", Now the two of them quote each other . Then repeat the experiments just now :
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | extern | × | × | Output 0 1 0 You can use √ |
| aim=1 | × | extern | × | Output 0 1 0 You can use √ |
| aim=1 | × | × | extern | Output 0 1 0 You can use √ |
| extern | aim=1 | × | × | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | aim=1 | extern | × | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | aim=1 | × | extern | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| extern | × | aim=1 | × | Output 0 1 0 You can use √ |
| × | extern | aim=1 | × | Output 0 1 0 You can use √ |
| × | × | aim=1 | extern | Output 0 1 0 You can use √ |
| extern | × | × | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | extern | × | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | × | extern | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
Define only aim=1
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | × | × | × | In function 'dec': 'aim' undeclared |
| × | aim=1 | × | × | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| × | × | aim=1 | × | In function 'add': 'aim' undeclared |
| × | × | × | aim=1 | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
stay .h Statement int aim, stay .c Instantiation int aim = 1
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | aim; | × | × | Output 0 1 0 You can use √ |
| aim=1 | × | aim=1 | × | b.o:multiple definition of "aim"; a.o:fisrt defiined here |
| aim=1 | × | × | aim; | Output 0 1 0 You can use √ |
| × | aim; | aim=1 | × | Output 0 1 0 You can use √ |
| × | aim; | × | aim; | Output -1 0 -1 Also available |
Ideal standardized writing :
| a.c | a.h | b.c | b.h | result |
|---|---|---|---|---|
| aim=1 | aim; | × | aim; | Output 0 1 0 You can use √ |
| aim=1 | aim; | × | extern aim; | Output 0 1 0 You can use √ |
| × | aim; | aim=1 | aim; | Output 0 1 0 You can use √ |
| × | extern aim; | aim=1 | aim; | Output 0 1 0 You can use √ |
5、 ... and . Analysis and usage
Tables are not all cases , But at least we can roughly understand .c Document and .h Scope of the file .
As the most convenient way to use :
- When a variable needs to be used in other files , Directly in other files
.huseexternJust make a statement ( Of course..cIt's fine too ). .hDo not instantiate , A simple statement is enough .- The same variable , When it
.hAfter declaration , The scope of this variable is in this.c and .hValid in scope , If by other documents.hAfter nesting , The scope of this variable extends from this file to this file and other files . If there is instantiation under two files , Then there are two instantiations in the same scope , But this scope has no order , So it exists “ meanwhile ” Assign two values to a variable , So it will remindmultiple definition
notes :
- Instantiation , Refer to
int aim = 2;. The assignment isaim = 2;The two are very different , Need to distinguish .
6、 ... and . summary
Declarations can expand the scope , So there can be multiple , Instantiation can only occur once .
边栏推荐
猜你喜欢

WCF introductory tutorial II

Configure flask

“尝鲜”元宇宙,周杰伦最佳拍档方文山将于7月25日官宣《华流元宇宙》

Simulation of transfer function step response output of botu PLC first-order lag system (SCL)

在线问题反馈模块实战(十四):实现在线答疑功能

NFT数字藏品系统开发:文学+数字藏品的碰撞

Examples of financial tasks: real-time and offline approval of three scenarios and five optimizations of Apache dolphin scheduler in Xinwang bank

【推荐系统经典论文(十)】阿里SDM模型

Network Trimming: A Data-Driven Neuron Pruning Approach towards Efficient Deep Architectures论文翻译/笔记

DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network
随机推荐
China Unicom transformed the Apache dolphin scheduler resource center to realize the one-stop access of cross cluster call and data script of billing environment
Speech at 2021 global machine learning conference
2019 ZTE touyue · model compression scheme
Compose Canvas line chart
从Boosting谈到LamdaMART
20220209 create a basic Servlet
NFT数字藏品系统开发:数字藏品赋予品牌新活力
Sort: merge sort and quick sort
HCIP---BGP综合实验
Singles cup web WP
Meta universe infrastructure: analysis of the advantages of Web 3.0 chain33
什么是消息订阅和发布?
tensorflow2.x中的量化感知训练以及tflite的x86端测评
Learn browser decoding from XSS payload
ModuleNotFoundError: No module named ‘pip‘解决办法
MMOE多目标建模
NFT digital collection development: Six differences between digital collections and NFT
Deep learning model deployment
基于Thinkphp的开源管理系统
系统架构&微服务