extern

extern的使用

£可爱£侵袭症+ 提交于 2019-11-30 22:57:48
一、extern声明变量和函数说明这个变量和函数已经在其他文件中定义了; 二、在C++中,extern “C” 表示该函数以C语言的方式编译; 三、全局变量和局部变量同名时,局部变量加上extern则使用这个局部变量。   问题1:C中全局变量局部变量能同名吗?怎么访问全局变量?   可以同名。   如何访问:1.通过指针        2.通过函数        3.用extern int a; //通过函数 int fun(){   return a; } int main(){   int *pa = &a;//通过指针   int a;   printf("%d\n",a);//局部变量   printf("%d\n",fun());   printf("%d\n",*pa);   int b = *pa;   //语句块 块变量   {     extern int a;//在当前语名块中的变量前加extern 相当于是全局的变量     printf("%d\n",a);   }   return 0; } 问题2:C++中怎么访问全局变量/函数?      定义在全局的变量或函数相当于无名名字空间       直接在前面加::   问题3:一个文件访问另一个文件的变量:       被访问的文件将该变量定义为全局变量 int a,在访问的文件中加extern访问

Extern and Static Pointers in C

旧街凉风 提交于 2019-11-30 19:58:30
Hi what could be the usage of static and extern pointer ?? if they exist To answer your question about when they could be used, a couple of simple examples: A static pointer could be used to implement a function that always returns the same buffer to the program, allocating it the first time it is called: char * GetBuffer() { static char * buff = 0; if ( buff == 0 ) { buff = malloc( BUFSIZE ); } return buff; } An extern (i.e. global) pointer could be used to allow other compilation units to access the parameters of main: extern int ArgC = 0; extern char ** ArgV = 0; int main( int argc, char **

__declspec(selectany)的作用

白昼怎懂夜的黑 提交于 2019-11-30 17:16:33
不懂就要问百度 转载自 http://blog.163.com/cumt_xl/blog/static/19071504420127114610861/ 最 近在用 template 编写singleton模式代码的时候,遇到了一个问题,template要求实现要在同一个文件中,所以,我只能在h文件中定义并实现 singleton 模式类。类中必然要有静态成员变量,静态成员变量的定义成了问题,如果我放在cpp文件中,模板是不支持的,放在h文件中,如果h文件被多次包含,会出现 重定义的情况。 回来,请教高手,得知,可以在初始化静态成员变量前面加上__declspec(selectany) ,这样编译器会自动剔除对该静态成员的重复定义。 最近半年也一直用WTL,ATL,COM等。其实在WTL,ATL中已经大量使用了__declspec(selectany)方法。我猜想这是为解决template单文件编程和静态成员变量在头文件中定义会出现重复定义矛盾而提出的。 总的来说: __declspec(selelctany) 使在头文件中定义静态成员变量可行。 =================================================================================== 其他资料: selectany使用在c/c++工程的连接期间

const 限定符

情到浓时终转凉″ 提交于 2019-11-30 16:27:13
const 限定符 const 对象一旦创建后不可改变,所以const必须初始化. const int i=get_size(); //运行时初始化 const int j=43; const int k; //错误,必须初始化 默认状态下,const对象仅在文件中有效,解决办法是 对于const变量不管是声明还说定义都添加extern关键字 extern const int bufSize=fcn(); //该常量能被其他文件访问 extern const int bufSize;//为同一个 下面那些句子是合法的,如果有不合法的句子,请说明为什么. const int buf; //必须初始化 int cnt=0; //正确 const int sz=cnt; //正确,拷贝赋值 ++cnt; //正确 ++sz; //常量对象不可改变 const 引用 引用绑定在const对象上,称为const引用 const int ci=1024; const int &r1=c1; //引用及对应的对象都是常量 const 指针 const double pi=3.14; const double *cptr=&p1; 顶层const 顶层const:指针本身是个常量 int i=0; int *const p1=&i; //不能改变p1的值,是顶层const 底层const

Why do I get “PInvokeStackImbalance was detected” for this simple example?

◇◆丶佛笑我妖孽 提交于 2019-11-30 13:11:14
问题 I'm creating a very simple PInvoke sample: extern "C" __declspec(dllexport) int Add(int a, int b) { return a + b; } [DllImport("CommonNativeLib.dll")] extern public static int Add(int a, int b); return NativeMethods.Add(a, b); But whenever I call the above NativeMethods.Add method I get the following managed debug assistant: PInvokeStackImbalance was detected Message: A call to PInvoke function 'CommonManagedLib!CommonManagedLib.NativeMethods::Add' has unbalanced the stack. This is likely

Linux C打印所有的环境变量

∥☆過路亽.° 提交于 2019-11-30 13:08:22
#include <stdio.h> extern char** environ; int main() { int nIndex = 0; for(nIndex = 0; environ[nIndex] != NULL; nIndex++) { printf("%s\n",environ[nIndex]); } } ———————————————— 版权声明:本文为CSDN博主「dmfrm」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u010889616/article/details/48133325 来源: https://www.cnblogs.com/nanqiang/p/11590366.html

Why does C++11 not support declaring extern “C” on a static member function?

冷暖自知 提交于 2019-11-30 08:27:26
Provided that I have a C library containing a function declared as void g(void (*callback)()); The following code is elegant yet illegal: struct A { // error C2159: more than one storage class specified (VC++ Nov 2012 CTP) static extern "C" void callback() {} }; g(A::callback); Why does C++11 not support this? This is a particularly confusing topic to wade into. Let's attack §7.5 "Linkage specifications" [dcl.link]. 1) All function types, function names with external linkage, and variable names with external linkage have a language linkage . Note that the property of language linkage applies

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

不问归期 提交于 2019-11-30 07:16: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 I'm trying so far: extern bool __cdecl TestFunc() { return true; } Originally I simply had this, but

Why do I get “PInvokeStackImbalance was detected” for this simple example?

半城伤御伤魂 提交于 2019-11-30 06:39:27
I'm creating a very simple PInvoke sample: extern "C" __declspec(dllexport) int Add(int a, int b) { return a + b; } [DllImport("CommonNativeLib.dll")] extern public static int Add(int a, int b); return NativeMethods.Add(a, b); But whenever I call the above NativeMethods.Add method I get the following managed debug assistant: PInvokeStackImbalance was detected Message: A call to PInvoke function 'CommonManagedLib!CommonManagedLib.NativeMethods::Add' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the