extern

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

半世苍凉 提交于 2019-12-18 12:55:28
问题 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? 回答1: 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

Linkage of various const/static variables

二次信任 提交于 2019-12-18 10:46:36
问题 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

Difference between putting variables in header vs putting variables in source

a 夏天 提交于 2019-12-18 07:02:57
问题 Say I declare a header file with a variable: int count; Then in the source file, I want to use count . Do I have to declare it as: extern int count Or can I just use it in my source file? All assuming that I have #include "someheader.h" . Or should I just declare it in the source file? What is the difference between putting count in the header file vs the source file? Or does it not matter? 回答1: You only want one count variable, right? Well this line: int count; Defines a count variable for

extern declaration, T* v/s T[]

孤街浪徒 提交于 2019-12-18 01:20:33
问题 I saw following piece of code in a legacy project. /* token.c */ struct token id_tokens[MAX_TOKENS]; /* analyse.c (v1) */ extern struct token *id_tokens; /* Raised my eyebrow, id_token declares a pointer */ I insisted on changing analyse.c to contain the declaration as below: /* analyse.c (v2) */ extern struct token id_tokens[]; /* I am happy with this. id_tokens declares array of unspecified size. */ I want v2 because pointer to T is not same as array of T . My friend's counter argumented

Pointer-array-extern question

為{幸葍}努か 提交于 2019-12-17 22:35:27
问题 File 1.c int a[10]; File main.c: extern int *a; int main() { printf("%d\n", a[0]); return 0; } Gives me a segfault! What's going wrong? 回答1: Arrays decompose, or are implicitly converted to pointers when passed to a function as an argument, or when converted to an r-value on the right-hand-side of the assignment operator. So something like: int array[10]; int* a = array; //implicit conversion to pointer to type int void function(int* a); function(array); //implicit conversion to pointer to

C++ extern keyword on functions. Why no just include the header file?

做~自己de王妃 提交于 2019-12-17 22:05:38
问题 If I understand it correctly this means extern void foo(); that the function foo is declared in another translation unit. 1) Why not just #include the header in which this function is declared? 2) How does the linker know where to look for function at linking time? edit: Maybe I should clarify that the above declaration is then followed by using the function foo(); It is never defined in this translation unit. 回答1: 1) It may not have a header file. But yes, in general, for large projects, you

Use of 'extern' keyword while defining the variable

偶尔善良 提交于 2019-12-17 20:56:46
问题 After seeing this answer I have this doubt. In my project, I have seen some extern variables declared and defined like below: file1.h extern int a; file1.c extern int a=10; But in the link I mentioned it says that in the c file it should be defined like: int a = 10; Does adding extern key word during the definition too has any purpose/meaning. Or does it matter by the way? 回答1: It does not change the meaning. extern only makes sense when you declare a variable. Defining a variable with extern

What is default storage class for global variables?

会有一股神秘感。 提交于 2019-12-17 18:33:16
问题 What is default storage class of a global variable? While searching on web I found, some sites say it is static . But, static means internal linkage and the variable can not be available outside the file scope i.e it should not be available to other object files. But, they still can be accessed to other files using declarations like extern int i . And, if I explicitly mention static to global variable then it is not available outside the file scope. Then, what is correct default storage class

How to call a C++ method from C? [duplicate]

跟風遠走 提交于 2019-12-17 16:28:17
问题 This question already has an answer here : Calling “C++” class member function from “C” code (1 answer) Closed 3 years ago . I have a C++ class and I'm compiling it with some C files. I want to call a function which is defined in C++, actually in C++ class, so what am I going to do? The following declarations to show what am I saying: there may there be syntax errors: serial_comm.cpp class MyClass { void sendCommandToSerialDevice(int Command, int Parameters, int DeviceId) { //some codes that

Why does initializing an extern variable inside a function give an error?

半城伤御伤魂 提交于 2019-12-17 15:47:25
问题 This code compiles fine: extern int i = 10; void test() { std::cout << "Hi" << i << std::endl; } While this code gives an error: void test() { extern int i = 10; std::cout << "Hi" << i << std::endl; } error: 'i' has both 'extern' and initializer I read this in C++ Primer: Any declaration that includes an explicit initializer is a definition. We can provide an initializer on a variable defined as extern, but doing so overrides the extern. An extern that has an initializer is a definition. It