当前位置:网站首页>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
边栏推荐
- NiO implementation
- 文件服务器FastDFS
- Huffman coding principle
- "Niuke | daily question" template stack
- MySQL table write lock
- Idea -- use @slf4j to print logs
- On stock price prediction model (3): are you falling into the trap of machine learning
- MySql 执行计划
- [database] CTE (common table expression)
- Realize the full link grayscale based on Apache APIs IX through MSE
猜你喜欢
随机推荐
"Final review" 16/32-bit microprocessor (8086) basic register
Multi-objective collaborative decision-making in supply chain
浅谈eval与assert一句话木马执行区别
MySQL execution plan
Deep learning learning notes -- solve the problem of slow download of CONDA and pip
buuReserve(4)
LeetCode刷题1:题目分类
[hard ten treasures] - 7.2 [dynamic RAM] analysis of the difference between DDR4 and DDR3
LTS(Light-Task-Scheduler)
Drools (2): drools quick start
< II> ObjectARX development: create and edit basic graphic objects
解决 Chrome 浏览器被毒霸篡改问题
File server fastdfs
LTS(Light-Task-Scheduler)
The results of the soft test can be checked, and the entry to query the results of the soft test has been opened in the first half of 2022
Common CMD instructions
MySQL isolation level transactions
[749. Isolate virus]
"Niuke | daily question" template stack
IR tool in JIT and download, compile and use of jitwatch









