extern

Unique address for constexpr variable

痞子三分冷 提交于 2019-11-30 02:30:33
问题 Is it possible to have a unique address allocated for a constexpr variable, i.e. the same for all translation units where the variable is available (usually through a header)? Consider the following example: // foo.hh #include <iostream> constexpr int foo = 42; // a.cc #include "foo.hh" void a(void) { std::cout << "a: " << &foo << std::endl; } // b.cc #include "foo.hh" extern void a(void); int main(int argc, char** argv) { a(); std::cout << "b: " << &foo << std::endl; } Compiling a.cc and b

Linkage of various const/static variables

自古美人都是妖i 提交于 2019-11-29 23:22:32
I have a few questions about the linkage from the following variables. By examples of 7.1.1/7 of C++03 and experimenting with compilers (Comeau, Clang and GCC), I came to the following linkage kinds: First static , then extern static int a; // (a) extern int a; // (b) valid, 'a' still internal It's clear to me with accordance to section 3.5: (a) implies internal linkage. And (b) also implies internal linkage, because the name "a" is declared static (by (a)). First extern , then static extern int b; // (c) static int b; // (d) invalid! First, (c) implies external linkage. But (d) implies

C#如何读写和创建INI文件

邮差的信 提交于 2019-11-29 22:07:49
在做项目过程中,有时需要保存一些简单的配置信息,可以使用xml,也可以使用INI文件。下面是C#中读取INI的方法,相信大部分朋友都使用过这种方式。 INI文件的存储方式如下, [section] key=value key=value 读取写入方法, [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString,

问题集锦(30-35)

蓝咒 提交于 2019-11-29 21:41:03
Problem 30 关于 C 和 C++ 混合编程问题? Ans : 如果 C++ 程序要调用已经被编译后的 C 函数,该怎么办? 假设某个 C 函数的声明如下: void foo(int x, int y); 该函数被 C 编译器编译后在库中的名字为 _foo ,而 C++ 编译器则会产生像 _foo_int_int 之类的名字用来支持函数重载和类型安全连接。由于编译后的名字不同, C++ 程序不能直接调用 C 函数。 C++ 提供了一个 C 连接交换指定符号 extern“C” 来解决这个问题。例如: extern “C” { void foo(int x, int y); … // 其它函数 } 或者写成 extern “C” { #include “myheader.h” … // 其它 C 头文件 } 这就告诉 C++ 编译译器,函数 foo 是个 C 连接,应该到库中找名字 _foo 而不是找 _foo_int_int 。 C++ 编译器开发商已经对 C 标准库的头文件作了 extern“C” 处理,所以我们可以用# include 直接引用这些头文件。 -------------------------------------------------------- 延深 ------------------------------------------------

[C++学习]变量的声明与定义

烂漫一生 提交于 2019-11-29 21:24:38
描述 C++ 语言支持分离式编译机制,该机制允许把程序分割为多个文件,每个文件可以独立编译。例如:std::cout和std::cin,它们定义与标准库中,但是却能被我们的程序使用。所以,为了支持该机制,C++ 语言将声明与定义区分开来。 声明(declaration)使得名字为程序所知,如果一个程序想使用另外一个文件中的定义变量,则需要包含对那个文件的声明;定义(definition)创建与名字关联的实体。 使用方法 声明一个变量需要在变量前添加extern,并且不能显式的初始化变量。 extern int i; // 声明i,但没有定义 int j; // 声明并且定义j 任何包含显式初始化的声明即成为定义。 extern double pi = 3.14159; // 定义 总结 如果要在多个文件中使用同一个变量,就必须将声明和定义分开; 任何包含显式初始化的声明即成为定义; 变量能且只能被定义一次,但是可以被多次声明;换言之变量的定义必须出现在且只能出现在一个文件中,而其他用到该变量的文件必须对其进行声明,却不能重复定义。 在函数体内部,如果试图初始化一个由extern关键字标记的变量,将引发错误。 来源: https://www.cnblogs.com/linzijie1998/p/11533197.html

Should functions be made “extern” in header files?

不想你离开。 提交于 2019-11-29 19:58:19
Should functions be made extern in header files? Or are they extern by default? For example, should I write this: // birthdays.h struct person find_birthday(const char* name); or this: // birthdays.h extern struct person find_birthday(const char* name); From The C Book : If a declaration contains the extern storage class specifier, or is the declaration of a function with no storage class specifier (or both), then: If there is already a visible declaration of that identifier with file scope, the resulting linkage is the same as that of the visible declaration; otherwise the result is external

C语言中的extern关键字

走远了吗. 提交于 2019-11-29 19:27:11
目录 概述 变量/函数的声明(declaration)与定义(definition) extern关键字 extern关键字与函数 extern关键字与变量 概述 extern关键字可以被用于修饰C变量和函数,他扩展变量和函数的可见性。 变量/函数的声明(declaration)与定义(definition) 声明(declaration) :变量/函数的声明只是宣布变量/函数存在于程序的某个地方,但是还没有为它们分配内存。当一个变量被声明,程序就会知道那个变量的数据类型。当一个函数被声明,程序就会知道函数的参数类型、参数顺序以及返回类型。 定义(definition) :定义一个变量/函数时,除了声明,还会为变量/函数分配内存。可以认为定义是声明的超集。 从上面的解释可以看到, 变量/函数可以被声明多次,但只能被定义一次 。 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。 extern关键字 extern关键字与函数 默认情况下,C函数的声明和定义会被自动加上extern。例如,当我们写如下代码 int foo(int arg1, char arg2); 编译器会看到 extern int foo(int arg1, char arg2);

Issue declaring extern class object

我是研究僧i 提交于 2019-11-29 16:32:53
Let me start by saying I've extensively searched for answers on google and more specifically here. The thing is I actually (at least I think I did) found people with similar problems, though the answer given to them gave me another problem. I'm using Visual Studio 2010 Express and working with SFML libary (though i do not think this last part is relevant) So here it goes: I have a source file called player.cpp which holds class Player and I have a header file (included in all source files) called cc.h(command and control) that holds all the necessary includes and external variables/functions.

Is extern “C” required also for linking global variables used in Cpp file to the one defined in a cfile?

我的梦境 提交于 2019-11-29 16:16:27
Is extern "C" required also for linking global variables used in Cpp file to the one defined in a c file? It is used for linking function from C++ file which is referenced in C file because of the name mangling of function names in C++ files. Does the C compiler also changes the name of variables?? Is extern "C" required also for linking global variables used in Cpp file to the one defined in a c file? Portably, yes. You might find that leaving out extern "C" works for your compiler (for example, GCC, which doesn't mangle C++ variable names in the global namespace), but that's not something

Under what circumstances can an extern variable be used in definition?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 16:16:26
I am very very sorry. I didn't know my incomplete code attachment would create such a mess. I am very glad to see so many sincere helps. This code will compile: int myadd(int, int); static int main_stat = 5; int main() { int i, j; main_stat = 13; j = myadd(-1,7); i = main_stat; cout << j << i; // 3 and 13 return 0; } myadd.cpp extern int main_stat = -3; int myadd(int x,int y) { int t = main_stat; t = x + y; y = t +main_stat; return y; // will return 3 } See I defined and extern linking main_stat. Why is that legal? I thought you could only link and not define. Is storage allocated in the stack