当前位置:网站首页>Why can't extern compile variables decorated with const?
Why can't extern compile variables decorated with const?
2022-07-26 07:06:00 【0x8g1T9E】
const Variables can be used by other files extern Do you quote ? Why? ?
Let's start with a piece of code :
// main.cc
#include<stdio.h>
// Reference externally defined const_int Variable
extern const int const_int;
int main()
{
printf("const_int:%d\n",const_int);
return 0;
}
// const.cc
// Definition const Variable
const int const_int = 10;
Compile Links :
$ g++ -o main main.cc const.cc
/tmp/ccWHAXxB.o: In function `main':
main.cc:(.text+0x6): undefined reference to `const_int'
collect2: error: ld returned 1 exit status
We found a link problem , say const_int There are no defined references , But we are http://const.cc It's defined in the file .
Let's get rid of const Modifier compile again , Discovery is possible . From the above compilation problem , That leads to what we are going to talk about today . As for why it compiles, but , Finally, explain .
Of course you will find , according to C Code to compile , It can be compiled . I'll explain later .
Link properties
We all know ,C/C++ The compilation of code is usually precompiled , assembly , compile , link ( Reference resources hello How the program is generated ) There are usually variables with three link attributes : External links , Internal link or no link .
Has a functional scope , Variables in block scope or function prototype scope are unlinked variables ; Variables with file scope can be internal links or external links . External link variables can be used in multiple files , Internal link variables can only be used in one compilation unit ( A source code file and the header file it contains ).
About scope , You can also refer to 《 Global variables , Static global variables , local variable , Static local variables 》.
Said so much , Take a specific example :
#include<stdio.h>
int external_link = 10; // File scope , External links
static internal_link = 20; // File scope , internal link
int main()
{
int no_link = 30; // No links
printf("%d %d %d \n",external_link,internal_link,no_link);
return 0;
}
It's easy to distinguish between unlinked variables , As long as it is not a variable of file scope , Basically, there is no link attribute . Is the file scope variable an internal link or an external link ? Just see if there is static Can be modified . Of course for C++, It also depends on whether there is const modification , Let's talk about it later .
How to know what link attribute a variable is ?
for instance , In the previous code , We are in accordance with the C Code to compile :
$ gcc -c const.c
$ nm const.o |grep const_int
0000000000000000 R const_int
nm Command in 《linux Common commands - Development and debugging 》 There is a little introduction , It can be used to see ELF Symbol information of the file .
From the results here we can see const_int The front is R Embellished ,
R: The symbol is located in the read-only data area ,READONLY The meaning of
and The letter is capitalized , In fact, it also means that it has external link properties .
Let's see according to C++ The code to compile :
$ g++ -c const.c
$ nm const.o |grep const_int
0000000000000000 r _ZL9const_int
You can see , Its modifier is also r, But it is lowercase r, Lowercase letters indicate that the variable has internal link properties .
nm The command is very practical , But this article is not the focus .
const keyword
Speaking of const keyword , stay 《const How to use keywords 》 and 《C++ Medium const And C Medium const What's the difference ?》 Has been analyzed in , Let's talk about it briefly , By const Keyword modifies the variable , Indicates that it is read-only , Do not want to be modified .
extern keyword
extern Keywords can refer to external definitions , Presumably many friends are already familiar with , for instance , If you take the const Keywords are removed ,http://main.cc Medium extern It means , That means there is one const_int Variable , But it is defined elsewhere , So here extern Decorate it , So in the link phase , It will find its definition in other compilation units , And link .
Of course , Another function that is less concerned is , stay C++ in , It can change const Link properties of variables .
Yes , stay C++ in , It changed const_int Link properties for . We can modify it const.c Is as follows :
extern const int const_int = 10;
And then check it out :
$ nm const.o |grep const_int
0000000000000000 R const_int
Found no , The decoration in front of it becomes capitalized R 了 , So at this point , You compile again , You can compile , It's not a mistake , about C, It is originally an external link attribute , Therefore, no error will be reported at all .
extern There is another use :
《C++ How to call C Interface 》?
Solve the problem
therefore , The common problem of link error is that the definition cannot be found , The reason is nothing more than :
- Undefined
- Defined elsewhere , But it does not have external link properties
- Defined , Have external link properties , But there is a problem with the order of links
Because in C++ in , By const The decorated variable defaults to the internal link attribute , Because the link will not find the definition .
summary
This article starts with a compilation problem , Led to a lot of content , Include :
- Scope --《 Global variables , Static global variables , local variable , Static local variables 》
- const keyword --《const How to use keywords 》
- extern keyword
- nm Look at the symbol table
边栏推荐
- Make a chase game with pyGame
- 屏:框贴、0贴合、全贴合
- NIO实现
- SQL shell (PSQL) tool under PostgreSQL
- MySQL intent lock
- Shared lock
- 20000 words will take you from 0 to 1 to build an enterprise level microservice security framework
- [hard ten treasures] - 7.2 [dynamic RAM] analysis of the difference between DDR4 and DDR3
- MySql 执行计划
- 「“xxxx“正在运行,可能导致系统卡顿,降低待机时间,点按关闭」处理
猜你喜欢

火焰图分析Flink反压

On the difference between Eval and assert

"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary question record (acdgij)

Leetcode question brushing 1: topic classification

opengauss简易版安装报错

Overview of new features of es11, ES12 and es13

20220725 compensator in automatic control principle

Log rotation logrotate

String and memory functions

Realization of LED water lamp based on C51
随机推荐
LTS(Light-Task-Scheduler)
Analysis of strong tennis cup 2021 PWN competition -- baby_ diary
Function of hot air pad
【QT】怎样获得QTableView和QTableWidget的行数和列数
"XXXX" is running, which may cause the system to jam, reduce the standby time, and click Close "
20220725 自动控制原理中的卷积Convolution
[untitled] reprint
Depth cloning and reflection of typescript class objects
你了解MySQL都包含哪些“零件“吗?
QT listens for socket events and uses qsocketnotifier class
Do you want to restart the database to replace the license?
Do you know what "parts" MySQL contains?
Idea -- use @slf4j to print logs
[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design
[Star Project] small hat aircraft War (IV)
软考可以查成绩了,2022年上半年软考成绩查询入口已开通
Check the top 10 best graphics software of the year, meet 99% of your chart needs, and collect it quickly
MySQL read / write lock
QT监听socket事件,使用QSocketNotifier类
Shell programming