extern

Why doesn't this “undefined extern variable” result in a linker error in C++17?

删除回忆录丶 提交于 2019-12-03 00:58:49
I have compiled and ran the following program in a C++17 compiler (Coliru). In the program, I declared an extern variable, but did not define it. However, the compiler doesn't give a linker error . #include <iostream> extern int i; // Only declaration int func() { if constexpr (true) return 0; else if (i) return i; else return -1; } int main() { int ret = func(); std::cout<<"Ret : "<<ret<<std::endl; } Why doesn't the compiler give a linker error? Because the variable isn't odr-used. You have a constexpr if there that always discards the branch that could use it. One of the points of constexpr

Difference between extern int a; extern int a=42;

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: While I was reading the answers of Use of 'extern' keyword while defining the variable One of the user answered these way extern int a; // not a definition extern int a = 42; // definition I was expecting both are not definitions but declarations. I was thinking Both statements says that the variable is defined outside the function and we have to use extern keyword to use it. is this a mistake by him or is it really a definition ? I know that extern int a; // variable is already defined but its outside the function extern int a=42 ; //I

extern NSString *const in a class.

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Hi I've got this header file: #import <Foundation/Foundation.h> @interface PCConstants : NSObject extern NSString *const kPCUserProfileKey; extern NSString *const kPCUserProfileNameKey; extern NSString *const kPCUserProfileFirstNameKey; extern NSString *const kPCUserProfileLocationKey; extern NSString *const kPCUserProfileGenderKey; extern NSString *const kPCUserProfileBirthDayKey; extern NSString *const kPCUserProfileInterestedInKey; @end implementation: #import "PCConstants.h" @implementation PCConstants NSString *const kPCUserProfileKey =

unable to load MediaInfo Library

匿名 (未验证) 提交于 2019-12-03 00:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Am using Mediainfo library in my C# project,before start invoking this dll,i just ran VC++ program that comes with the package and deployed in to my local system.am not sure what exactly it did when it deploys,it register something in the system,so my process can identify the mediainfo.dll,so it worked well in my local (32bit). When i start deploying in Testserver,i can't run or deploy the VC++(bcoz test server don't have visual studio to do that,except debugger).so am getting this error public class MediaInfo { //Import of DLL functions. DO

How to use extern “C” dll function taking char** as an argument in C# application?

匿名 (未验证) 提交于 2019-12-03 00:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have dll with function: extern "C" int doJob(char** buffer); Its usage with C++ looks like this: char* buf; int status = doJob(&buf); What definition should I have for this function in C#? How can I use this function in C#? 回答1: One of the possible patterns is: [DllImport("containsdojob.dll", CallingConvention = CallingConvention.Cdecl)] public static extern Int32 doJob(out IntPtr buffer); [DllImport("containsdojob.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void freeMemory(IntPtr buffer); and IntPtr buffer =

c语言-extern

匿名 (未验证) 提交于 2019-12-03 00:37:01
函数在使用外部变量之前,必须要知道外部变量的名字。需要在使用的外部变量前加extern。 在源文件中,外部变量在函数之前,可以省略extern关键字。 转载请标明出处: c语言-extern 文章来源: c语言-extern

【C++】:const限定符 &amp;

匿名 (未验证) 提交于 2019-12-03 00:22:01
const int MaxSize = 100 //定义一个常量 MaxSize = 44 //试图修改一个常量,系统会报错 若想要在其他文件中使用这个const变量,则需要在定义的时候加上extern Note: 非const变量默认是extern。因此不需要再变量前面添加extern //file1.cpp extern const int MAX_COUNT = 20 //file2.cpp extern const int MAX_COUNT; //使用file1中的MAX_COUNT。 文章来源: 【C++】:const限定符 &

全局变量能否放在头文件中定义――NO

匿名 (未验证) 提交于 2019-12-03 00:22:01
能不能在头文件中定义全局变量? 。总结起来就是,报错的原因就是有两个CPP,各自生成自己的OBJ,那么在查找符号的时候,都能发现对方那里也有一个变量a,那么就报错了。所以,如果你能保证你的头文件只被包含一次,那么可以在其中定义此外链接性的变量,如果无法保证的话,引用作者的话“ ”。这种方法如果写起来makefile应该也不难,历来公共的接口、头文件都是放在一起,此用于 共享全局变量的头文件放在这里 ,正常写就好了。 PS:定义的概念,不加extern或者进行了初始化都视为定义。声明只有extern type-name a一种 文章来源: 全局变量能否放在头文件中定义――NO

iOS 静态、全局变量、常量

匿名 (未验证) 提交于 2019-12-03 00:09:02
关键字static 两个概念:生命周期、作用域 生命周期 :这个变量能存活多久,它所占用的内存什么时候分配,什么时候收回。 作用域 :这个变量在什么区域是可见的,可以拿来用的。 static 分两种情况:修饰局部变量、修饰全局变量 1、 static 修饰局部变量 局部变量:在函数/方法/代码块内声明的变量。它的生命周期、作用域都是在这个代码块内。 局部变量 存储在栈区(stack) 一旦出了这个代码块,存储局部变量的这个栈内存就会被回收,局部变量也就被销毁。 当用 static 修饰局部变量时,变量被称为 静态局部变量 ,和全局变量,静态全局变量一样,是存储在‘静态存储区’。 存储在 静态存储区 的变量,其内存直到 程序结束 才会被销毁。 即,生命周期是整个源程序。 静态局部变量 的生命周期是整个源程序,但,作用域是声明它的代码块内。 2、 static 修饰全局变量 当全局变量没有使用 static 修饰时 其存储在静态存储区,直到程序结束才销毁。也就是其作用域是整个源程序。 我们可以使用 extern 关键字来引用这个全局变量。 当全局变量使用 static 修饰时 其生命周期没有变,依旧是在程序结束时才销毁。但是其作用域变了。现在只限于申明它的这个文件才可见。 使用 extern 关键字无法引用这个全局变量。 全局变量本来是在整个源程序的所有文件都可见, static

【M34】如何在同一个程序中结合C++和C

匿名 (未验证) 提交于 2019-12-02 23:54:01
1、C++和C混合编程的时候,需要考虑产生的目标文件的兼容性。 2、名称重整,为什么要搞出名称重整? 连接器要求所有方法名必须独一无二。对于C语言,没问题。C++支持过载,也就是方法名相同,形参表不同的方法,因此编译器编译的时候,必须对方法名重整,保证方法名独一无二,满足连接器的要求。那么问题来了,C++和C混合编程,编译时没有问题,连接时出现问题了,C++进行了名称重载,而C没有,连接时名称对不上了。   怎么解决这个问题?就是使用extern C,告诉编译器,不要进行名称重整。对于C++与C共用的头文件,C++必须使用extern C,而C语言不识别extern C,因此使用预编译。对于__cplusplus,使用externC,否则不使用extern C 3、statics的初始化,我们通常认为main方法是程序的入口点。实际上,在main方法执行之前要做一些准备工作,在main方法执行之后要做一些清除工作。   main方法之前:对于static对象,全局对象,命名空间和文件作用域的对象,执行构造方法,进行初始化。注意两点:第一,不同编译单元内的静态对象初始化顺序是不确定的。第二,对于局部static对象(方法内的对象),只有在方法被调用的时候初始化,而且是只有第一次调用的时候初始化,以后调用都不再初始化。   在main方法执行之后:要对上述的statics对象