extern

extern storage class specifier

泄露秘密 提交于 2019-11-28 05:42:36
问题 Section 7.1 of the C++ Standard mentions about 'extern' as a storage class specifier. N3126 - "The extern specifier can be applied only to the names of variables and functions. The extern specifier cannot be used in the declaration of class members or function parameters. For the linkage of a name declared with an extern specifier, see 3.5. [ Note: The extern keyword can also be used in explicit-instantiations and linkage-specifications, but it is not a storage-class-specifier in such

What does the extern keyword mean?

谁说胖子不能爱 提交于 2019-11-28 04:28:03
What does the extern keyword mean? I've seen that in front of an function declaration like extern void DoFoo ... extern gives a name external linkage . This means that the object or function is accessible through this name from other translation units in the program. For functions, this is the default linkage in any case so its usage (in this context) is usually redundant. Romain Hippeau The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern

static const Vs extern const

别说谁变了你拦得住时间么 提交于 2019-11-28 04:11:40
I have been using static const in my header files as so: static NSString * const myString = @"foo"; But have read that this is not the 'safe' or correct way of doing this. Apparently, if I want my const strings to be accessed from another class, I should be declaring the string in my .h as: extern NSString * const myString; Then in my .m file: NSString * const myString = @"foo"; Is this correct? If so, what is the reason not to declare it as static directly in my .h file? It works perfectly fine, and I can not see any 'safety' issues around this. It is a const, so therefore it can not be

extern const char* const SOME_CONSTANT giving me linker errors

╄→гoц情女王★ 提交于 2019-11-28 03:21:19
问题 I want to provide a string constant in an API like so: extern const char* const SOME_CONSTANT; But if I define it in my static library source file as const char* const SOME_CONSTANT = "test"; I'm getting linker errors when linking against that library and using SOME_CONSTANT: Error 1 error LNK2001: unresolved external symbol "char const * const SOME_CONSTANT" (?SOME_CONSTANT@@3QBDB) Removing the pointer const-ness (second const keyword) from both the extern const char* const declaration and

Can't understand the declaration #3 in the Example of [basic.link]/6 C++14

泄露秘密 提交于 2019-11-28 02:49:57
问题 [basic.link]/6 The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed.

C语言中static extern的使用

ⅰ亾dé卋堺 提交于 2019-11-28 01:32:28
10:30:22 2019-08-20 基础不牢 瞬间爆炸 参考资料: https://blog.csdn.net/ts_54eagle/article/details/4418627 https://blog.csdn.net/xingjiarong/article/details/47656339 https://blog.csdn.net/xingjiarong/article/details/47656339 看看C Primer Plus上是怎么定义的 先说说作用域 分为4种:块作用域 函数作用域 函数原型作用域 文件作用域 这里要说明的是 块作用域指{}之间的部分 而函数作用域单单指goto标签 当goto标签出现在函数内部时 goto标签作用域蔓延至整个函数 函数原型作用域从形参定义到原型声明结束为止 文件作用域指的是 该文件 该文件 该文件 从变量定义到文件末尾都可见 链接分为3种:外部链接 内部链接 无链接 (决定了是否能被外部文件使用) 块作用域 函数作用域 函数原型作用域的变量 都属于无链接 文件作用域的变量 可以属于外部链接 也可属于内部链接 内部链接的文件作用域 称为 文件作用域(只能在该文件内部使用) 外部链接的文件作用域 称为 全局作用域(整个程序都可使用) 一个具有文件作用域的变量默认是具有全局作用域的 在前面加上static变为具有文件作用域

【转贴】C、C++混合调用

对着背影说爱祢 提交于 2019-11-28 00:52:04
在项目中,C和C++代码相互调用是很常见的,但在调用时,究竟应该如何编写代码和头文件,有一些讲究,不然就可能出现编译时链接不通过的问题,典型的编译错误日志是: undefined reference to `xxx' 要编写出C或C++都能正常调用的代码,需要明白编译器在编译时,究竟做了什么。下面就以几段简单的代码为例,来说明一下GCC系列编译器在编译C、C++代码时,分别做了什么,我们该如何编写自己的函数库以供C和C++代码调用。 本文验证的环境是:Ubuntu Server 18.04 LTS,gcc/g++ 7.3.0,nm 2.30 C函数库如何被C和C++代码调用 sum.c是一个使用C代码编写的对整数求和函数,代码非常简单: 1 #include "sum.h" 2 3 int sum(int a, int b) 4 { 5 return a + b; 6 } 在不考虑C++的调用时,头文件sum.h通常会按照如下写法: 1 #ifndef __SUM_H__ 2 #define __SUM_H__ 3 4 int sum(int a, int b); 5 6 #endif /* __SUM_H__ */ 我们编写下面的main.cpp代码来调用它: 1 #include <iostream> 2 3 #include "sum.h" 4 5 int main(void

简单函数绘图语言解释器:语义分析器兼主程序

人盡茶涼 提交于 2019-11-28 00:18:30
semantic 一、基本思路 二、代码实现 三、运行结果 如果你要读诗,那么就要读自己的诗 :创造是极客唯一的属性 一、基本思路 语义分析是绘图语言的最后一步了。 语义分析器的核心:根据语义规则产生语义动作。 根据语法制导的基本思想,语义分析可以在语法分析过程得到实现。 对坐标设置语句(三种),语法分析识别通过之后,立刻计算表达式的值,并据此改写相应的全局变量。 对绘图语句(一种),语法分析识别通过之后,根据相应全局变量的设定值,立刻调用绘图函数绘制图形。 说明:由于本项目没有采用面向对象的设计方法,所以语义分析和语法分析是耦合在一起的,建议有兴趣的同学最好采用类来编程。 二、代码实现 语义分析头文件semantic.h # pragma once # ifndef SEMANTIC_H # define SEMANTIC_H # include "scanner.h" # include <graphics.h> # include <conio.h> /*语法制导翻译的中心思想是: 识别一条合法的语句,立刻对其翻译, 故需要向语法分析器加入语义分析的相关语句和函数 */ //全局变量区 extern struct Token token ; extern double Parameter ; extern double Origin_x , Origin_y ; extern

The usage of extern in c++

∥☆過路亽.° 提交于 2019-11-27 23:18:48
问题 I've having difficulty understanding how 'extern' works. I've searched Google but there doesn't seem to be the particular example case I'm trying If i have a file main.cpp which references one.h and in it i have a list named LIST1 (which is a double array of 100 x 100) so I have double List1[100][100]; how can i use this list in one.cpp please? extern double LIST1[100][100] is not working :/ main.cpp: #include "one.h" extern double LIST1[100][100]; one.cpp: void one::useList() { for(j = 0; j

Mixing extern and const

白昼怎懂夜的黑 提交于 2019-11-27 20:58:45
Can I mix extern and const, as extern const ? If yes, does the const qualifier impose it's reign only within the scope it's declared in or should it exactly match the declaration of the translational unit it's declared in? I.e. can I declare say extern const int i; even when the actual i is not a const and vice versa? Yes, you can use them together. And yes, it should exactly match the declaration in the translation unit it's actually declared in. Unless of course you are participating in the Underhanded C Programming Contest :-) The usual pattern is: file.h: extern const int a_global_var;