extern

Header file and extern keyword

a 夏天 提交于 2019-12-02 05:15:39
问题 I am having a lot of issue using extern variable and header files. I have read through sections of books and searched the web for hours but I haven't been able to figure out. Any help in understanding this problem will be greatly appreciated. The following is the code and the error when I try to compile #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sample.h" int main() { int i; int gI = 0; double recv; i = 10; gI = i; recv = AnotherFunc(); return 0; } And the sample.h

[c++11]宏

ぐ巨炮叔叔 提交于 2019-12-02 05:10:29
extern “C”的作用如下: 核心作用:实现C和C++的混合编程。extern “C”提供一个链接交换指定符号,用于告诉C++这段函数是C函数,extern “C”后面的函数不使用C++的名字修饰,而是使用C。 C++支持函数重载,C不支持函数重载。函数被C++编译后在库中的名字与C语言不同。如void add(int a, int b),该函数在C编译器编译后,库中名字为_add,而C++编译器则会生成add_int_int的名字。故C++提供C链接交换指定符号extern “C”来解决名字匹配> 问题。 被extern “C”限定的函数或变量是extern类型,extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,此关键字告诉编译器,该声明的函数可以在本模块或其它模块使用。被extern “C”修饰的变量和函数按照C语言方式编译和链接。 与extern对应的关键字是static,被static修饰的全局变量和函数只能在本模块中使用。如果一个函数或变量只能在本模块中使用时,不能用extern “C”修饰。 #include <iostream> using namespace std; int main(void) { cout << "__FILE__:" << __FILE__ << endl;//当前的文件名(绝对路径):C:\Users\xxx

Accesing global variable giving linker error in objective C

烈酒焚心 提交于 2019-12-02 04:12:30
I have declared a global variable like below extern NSString *name; @interface viewcontrollerOne{} in implementation file i am accessing that global variable in some method like -(void)someMethod { name = @"hello"; } but this is giving linker error. "name", referenced from: -[viewcontrollerOne someMethod] in viewcontrollerOne.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) The following is merely a declaration: extern NSString * const name; // << side note: this should typically be const It declares there is a

Why do enumeration constants have no linkage?

寵の児 提交于 2019-12-02 03:56:06
I'm trying to understand linkage of enumeration constant s and could not find a clear answer in the Standard N1570 . 6.2.2(p6) : The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern . So I need to understand that constants are not objects. Object is defined as 3.15 : region of data storage in the execution environment, the contents of which can represent values Also 6.2.2(p4) (emphasize mine

Using extern keyword to call functions

和自甴很熟 提交于 2019-12-02 03:41:49
I want to call functions defined in test.c from other.c. Can I extern the function1 to call it? Also, do I have to use extern in function2 and function3 , which are being called by function1 ? other.c extern function1(); function1(); test.c void function1() { function2(); function3(); } void function2() { } void function3() { } Function declarations are "by-default" extern . Quoting C11 , chapter §6.2.2, ( emphasis mine ) If the declaration of an identifier for a function has no storage-class specifier , its linkage is determined exactly as if it were declared with the storage-class specifier

C—变量

江枫思渺然 提交于 2019-12-02 03:16:37
C—变量 在C语言中,变量要先定义后使用。 使用时,必须说明变量的存储类型与数据类型。 变量说明的一般形式: <存储类型> <数据类型> <变量名>   存储类型的关键词有 auto、register、static、extern   数据类型可以是基本数据类型,也可以是自定义的数据类型。 auto与register对于自动存储,局部变量,在进入声明该变量的程序块时被建立,在程序活动时存在,退出该程序块时撤销,static与extern/对于静态存储。    auto 局部变量,默认为随机值  eg: auto int k; auto可省略。    register    Tips   1.寄存器变量可以用来优化加速c语言程序   2.声名只需在类型前多加register 即可,eg register int quick; (quick 就是一个整形的寄存器变量)   3.register只是一个建议型关键字,能不能声名成功还取决于编译器(建议型的关键字还有c++中的 inline),若不幸没有请求成功,则变量变成一个普通的自动变量。   4.是无法对一个register变量取地址的(因为寄存器变量多放在寄存器而非内存中,内存有地址,而寄存器是无地址的)   5.即便没有请求成寄存器变量,没有如愿的放入寄存器中,但是,依然不能对他取地址,因为他已经被声明为register了  

C++ “namespace scope”

依然范特西╮ 提交于 2019-12-02 02:33:29
The C++ spec ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf , section 7.5.4) states that A linkage-specification shall occur only in namespace scope What exactly does "namespace scope" mean? Does this mean that a linkage-specification, such as extern "C" can't be in global scope, only in a namespace? What exactly does "namespace scope" mean? It means, that extern "C" should be only in namespace-scope (not class-scope, block-scope etc.) Something, that is not in namespace, but is in global scope - is in global namespace scope. The potential scope denoted by an original

Using extern keyword to call functions

旧巷老猫 提交于 2019-12-02 02:26:55
问题 I want to call functions defined in test.c from other.c. Can I extern the function1 to call it? Also, do I have to use extern in function2 and function3 , which are being called by function1 ? other.c extern function1(); function1(); test.c void function1() { function2(); function3(); } void function2() { } void function3() { } 回答1: Function declarations are "by-default" extern . Quoting C11 , chapter §6.2.2, ( emphasis mine ) If the declaration of an identifier for a function has no storage

C++ “namespace scope”

怎甘沉沦 提交于 2019-12-02 02:23:31
问题 The C++ spec (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf, section 7.5.4) states that A linkage-specification shall occur only in namespace scope What exactly does "namespace scope" mean? Does this mean that a linkage-specification, such as extern "C" can't be in global scope, only in a namespace? What exactly does "namespace scope" mean? 回答1: It means, that extern "C" should be only in namespace-scope (not class-scope, block-scope etc.) Something, that is not in

Why 'extern' storage class works differently in functions?

南楼画角 提交于 2019-12-02 02:21:35
The following snippet works fine extern int i; int i; int main(){ return 0; } Here what I got is, 'i' is declared and then defined. Since there is only one definition so thats perfectly fine. int main(){ extern int i; int i; return 0; } Now, the above one gives the following error new.cpp: In function ‘int main()’: new.cpp:5:6: error: redeclaration of ‘int i’ int i; ^ new.cpp:4:13: note: previous declaration ‘int i’ extern int i; Whats the problem here? Here also there is single definition of 'i'. To understand the difference, you need to get familiar with a concept called tentative definition