extern

What is the behavior when there are mismatched types between an extern declaration and the definition?

大憨熊 提交于 2019-11-29 15:22:32
Suppose I have two files: ==File1== extern char* foo; ==File2== double foo; These two files seem to compile and link just fine with both g++ and clang++ despite the type mismatch. As I understand it the recommended practice is to put the extern declaration in a header which both files include so File2 will throw a redefinition error. My questions are: Does this result in undefined behavior according to the c++ standard? If not what goes in foo in File1? Could linkers catch this kind of type mismatch? Does this result in undefined behavior according to the c++ standard? Well, the real question

Two variables with same name and type, in two different .c files, compile with gcc

自作多情 提交于 2019-11-29 13:38:14
问题 Here's the deal. I've had two identical global variables in two different .c files, they weren't declared as extern. So each .c file should have seen its own variable, right? But I have gotten some really strange behaviour, as if one file was reading the other files variable (after linking them together). Adding 'static' qualifier to both variables definitions seemed to fix this issue. So what I'm actually wondering is, what exactly happened there without the 'static' qualifier? 回答1: So each

extern "C"的用法解析

你离开我真会死。 提交于 2019-11-29 13:26:44
extern "C" 的用法解析 http://blog.sina.com.cn/u/494a1ebc010004g5 C++ 中 extern “C” 含义深层探索 1. 引言 C++ 语言的创建初衷是 “a better C” ,但是这并不意味着 C++ 中类似 C 语言的全局变量和函数所采用的编译和连接方式与 C 语言完全相同。作为一种欲与 C 兼容的语言, C++ 保留了一部分过程式语言的特点(被世人称为 “ 不彻底地面向对象 ” ),因而它可以定义不属于任何类的全局变量和函数。但是, C++ 毕竟是一种面向对象的程序设计语言 ,为了支持函数的重载, C++ 对全局函数的处理方式与 C 有明显的不同。 2. 从标准头文件说起 某企业曾经给出如下的一道面试题: 面试题 为什么标准头文件都有类似以下的结构? #ifndef __INCvxWorksh #define __INCvxWorksh #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __cplusplus } #endif #endif /* __INCvxWorksh */ 分析 显然,头文件中的编译宏 “#ifndef __INCvxWorksh 、 #define __INCvxWorksh 、 #endif” 的作用是防止该头文件被重复引用。 那么

C++项目中的extern "C" {}

百般思念 提交于 2019-11-29 13:26:32
引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: 1 2 3 4 5 6 7 8 9 #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __cplusplus } #endif 它到底有什么用呢,你知道吗?而且这样的问题经常会出现在面试or笔试中。下面我就从以下几个方面来介绍它: 1、#ifdef _cplusplus/#endif _cplusplus及发散 2、extern "C" 2.1、extern关键字 2.2、"C" 2.3、小结extern "C" 3、C和C++互相调用 3.1、C++的编译和连接 3.2、C的编译和连接 3.3、C++中调用C的代码 3.4、C中调用C++的代码 4、C和C++混合调用特别之处函数指针 1、#ifdef _cplusplus/#endif _cplusplus及发散 在介绍extern "C"之前,我们来看下#ifdef _cplusplus/#endif _cplusplus的作用。很明显#ifdef/#endif、#ifndef/#endif用于条件编译,#ifdef _cplusplus/#endif _cplusplus——表示如果定义了宏_cplusplus,就执行#ifdef/#endif之间的语句,否则就不执行。 在这里为什么需要#ifdef

C++中extern “C”含义深层探索

╄→гoц情女王★ 提交于 2019-11-29 13:26:20
首先,作为extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,该关键字告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。 通常,在模块的头文件中对本模块提供给其它模块引用的函数和全局变量以关键字extern声明。例如,如果模块B欲引用该模块A中定义的全局变量和函数时只需包含模块A的头文件即可。这样,模块B中调用模块A中的函数时,在编译阶段,模块B虽然找不到该函数,但是并不会报错;它会在连接阶段中从模块A编译生成的目标代码中找到此函数 extern "C"是连接申明(linkage declaration),被extern "C"修饰的变量和函数是按照C语言方式编译和连接的,来看看C++中对类似C的函数是怎样编译的: 作为一种面向对象的语言,C++支持函数重载,而过程式语言C则不支持。函数被C++编译后在符号库中的名字与C语言的不同。例如,假设某个函数的原型为: void foo( int x, int y ); 该函数被C编译器编译后在符号库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字(不同的编译器可能生成的名字不同,但是都采用了相同的机制,生成的新名字称为“mangled name”)。 _foo_int_int 这样的名字包含了函数名、函数参数数量及类型信息,C++就是靠这种机制来实现函数重载的。例如,在C+

extern “C”的作用详解

99封情书 提交于 2019-11-29 13:26:06
extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码。加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C++的。由于C++支持函数重载,因此编译器编译函数的过程中会将函数的参数类型也加到编 译后的代码中,而不仅仅是函数名;而C语言并不支持函数重载,因此编译C语言代码的函数时不会带上函数的参数类型,一般之包括函数名。 这个功能十分有用处,因为在C++出现以前,很多代码都是C语言写的,而且很底层的库也是C语言写的,为了更好的支持原来的C代码和已经写好的C语言库,需要在C++中尽可能的支持C,而extern "C"就是其中的一个策略。 这个功能主要用在下面的情况: 1、C++代码调用C语言代码 2、在C++的头文件中使用 3、在多个人协同开发时,可能有的人比较擅长C语言,而有的人擅长C++,这样的情况下也会有用到 给出一个我设计的例子: moduleA、moduleB两个模块,B调用A中的代码,其中A是用C语言实现的,而B是利用C++实现的,下面给出一种实现方法: //moduleA头文件 #ifndef __MODULE_A_H //对于模块A来说,这个宏是为了防止头文件的重复引用 #define __MODULE_A_H int fun(int, int); #endif //moduleA实现文件moduleA.C /

C++ 学习笔记(一)

百般思念 提交于 2019-11-29 11:26:09
目录 变量的声明与定义 const 限定符 变量的声明与定义 声明关键字 extern extern int a; //声明 int a; //声明并定义 extern int a = 1; //定义 变量可以被多次声明,仅可定义一次 若想在多个文件中同时应用const 对象,需要在const前加extern extren const int a = 100; const 限定符 对const 的引用(简称常量引用) 例: int i = 42; const int &r1 = i; const int &r2 = 42; const int &r3 = r1 * 2; 常量引用可以对非常量执行,但是无法修改引用值,也不能通过引用值修改常量. 指向常量的指针 const 可以令指针指向常量或非常量. 不过指针不能修改所指对象的值. const double pi = 3.14; const double *cptr = π printf("%.2lf\n",*cptr); double dval = 3.16; cptr = &dval; printf("%.2lf\n",*cptr); output: 3.14 3.16 所谓指向常量的指针,仅仅是指不能通过该指针改变常量的值,但是可以通过其他方式改变 const指针 即指针是对象,把指针本身定位常量 即指针本身指向不可更改

extern const char* const SOME_CONSTANT giving me linker errors

假如想象 提交于 2019-11-29 09:49:30
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 the definition makes it work. How can I export it with pointer const-ness? The problem could be that the

How to set up a C++ function so that it can be used by p/invoke?

*爱你&永不变心* 提交于 2019-11-29 09:24:59
问题 Hopefully this is a brainlessly easy question, but it shows my lack of expertise with C++. I'm a C# programmer, and I've done extensive work with P/Invoke in the past with other people's C++/C dlls. However, this time I've decided to write a wrapper C++ dll (unmanaged) myself, and am then calling my wrapper dll from C#. The problem I am immediately running into is that I am unable to define a C++ function that can be found by p/invoke. I don't know what the syntax for this is, but here's what

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

。_饼干妹妹 提交于 2019-11-29 09:22: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. Otherwise, if no matching entity is found, the block scope entity receives external linkage.[ Example: