Questions tagged [extern]

extern is an access-specifier in C and C++ which defines a global variable that is visible to all object modules.

The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. The extern declaration in C/C++ is to indicate the existence of, and the type of, a global variable or function. A global variable, or a global function, is one that is available to all C/C++ modules. An extern is something that is defined externally to the current module.

schematic representation of global variable declared with extern keyword (source)

1475 questions
16
votes
2 answers

How to define extern variable along with declaration?

Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a…
haccks
  • 104,019
  • 25
  • 176
  • 264
16
votes
2 answers

When to use UIKIT_EXTERN vs just extern

I'm guessing I would only use UIKIT_EXTERN if there is a chance of C++ code in my project that may use the variable. If this is the case wouldn't it just be safe to declare all your externally available constants with UIKIT_EXTERN? How come I don't…
Ben Coffman
  • 1,750
  • 1
  • 18
  • 26
16
votes
3 answers

Can you extern a #define variable in another file?

For example abc.c contains a variable #define NAME "supreeth" Can extern the variable NAME in def.c?
user1295872
  • 461
  • 1
  • 6
  • 16
16
votes
2 answers

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

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"…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
15
votes
1 answer

Linking extern variables in C

In Unix, I have got three main files. One of them is a library and the other one is a program. MyLib.c and MyLib.h are the library. main.c is the program. In MyLib.h I have a declaration (extern int Variable;). When I try to use Variable in…
José M. Gilgado
  • 1,314
  • 3
  • 14
  • 21
15
votes
4 answers

Two variables with same name and type, in two different .c files, compile with gcc

Here's the deal. I've had two identical global variables in two different .c files, they weren't declared as extern. So each .c file should have seen its own variable, right? But I have gotten some really strange behaviour, as if one file was…
Ivan Š
  • 1,024
  • 2
  • 10
  • 15
14
votes
1 answer

Can't understand the declaration #3 in the Example of [basic.link]/6 C++14

[basic.link]/6 The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type,…
Leon
  • 868
  • 4
  • 12
14
votes
3 answers

extern on function prototypes?

my_math.h // case 1 unsigned int add_two_numbers(unsigned char a, unsigned char b); //case 2 extern unsigned int add_two_numbers(unsigned char a, unsigned char b); What is the difference between case 1 and case 2? I never used extern for…
CalinTamaian
  • 269
  • 1
  • 3
  • 7
14
votes
3 answers

Unique address for constexpr variable

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 constexpr…
MvG
  • 57,380
  • 22
  • 148
  • 276
13
votes
1 answer

Declare a C++ function that has C calling convention but internal linkage

I'm trying to interface with a C library, which expects me to provide a pointer to a callback function. As I understand it, according to the standard the callback must have C language linkage, due to possibly different calling convention. I can…
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
13
votes
1 answer

What happens if you nest extern "C"?

It looks like nesting extern "C" is legal. For example: extern "C" extern "C" void foo(); The second extern "C" is essentially ignored. Is that guaranteed by the C++ standard? Where?
calvin
  • 567
  • 4
  • 16
13
votes
4 answers

C++ program using a C library headers is recognizing "this" as a keyword. Extern "C" error?

My C++ program needs to use an external C library. Therefore, I'm using the extern "C" { #include } syntax for every module I need to use. It worked fine until now. A module is using the this name for some variables in one of…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
13
votes
2 answers

Use of extern in block scope

clang, gcc and VS2013 all complain about redefinition of w in main(), but I couldn't find in the Standard anything disallowing this. namespace N { extern int j; int j; } int main() { extern int w; int w; } These paragraphs say…
Wake up Brazil
  • 3,421
  • 12
  • 19
13
votes
4 answers

C#: Implementation of, or alternative to, StrCmpLogicalW in shlwapi.dll

For natural sorting in my application I currently P/Invoke a function called StrCmpLogicalW in shlwapi.dll. I was thinking about trying to run my application under Mono, but then of course I can't have this P/Invoke stuff (as far as I know…
Svish
  • 152,914
  • 173
  • 462
  • 620
12
votes
2 answers

Will a function declared inside main() have external linkage or none linkage?

See the following code: /* first file */ int i; /* definition */ int main () { void f_in_other_place (void); /* declaration */ i = 0 return 0; } /* end of first file */ /* start of second file */ extern int i; /* declaration */ void…
LocalHost
  • 910
  • 3
  • 8
  • 24