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
43
votes
2 answers

static and extern global variables in C and C++

I made 2 projects, the first one in C and the second one in C++, both work with same behavior. C project: header.h int varGlobal=7; main.c #include #include #include "header.h" void function(int i) { static int a=0; …
Cristi
  • 1,195
  • 6
  • 17
  • 24
42
votes
1 answer

What's the difference between static inline, extern inline and a normal inline function?

What's the difference between a static inline, extern inline and a normal inline function? I've seen some vague explanations about this. As far as I've understood, static inline is not just an inline function that is meant to only be referred to…
Forever a noob
  • 689
  • 1
  • 9
  • 16
41
votes
6 answers

Is extern "C" only required on the function declaration?

I wrote a C++ function that I need to call from a C program. To make it callable from C, I specified extern "C" on the function declaration. I then compiled the C++ code, but the compiler (Dignus Systems/C++) generated a mangled name for the…
bporter
  • 3,572
  • 4
  • 24
  • 23
40
votes
5 answers

Objective-C static, extern, public variables

I want to have a variable that I can access anywhere by importing a header file but I also want it to be static in the sense that there is only one of them created. In my .m file, I specify static BOOL LogStuff = NO; and in the initialize method I…
JPC
  • 8,096
  • 22
  • 77
  • 110
40
votes
2 answers

How to name a constant in Objective-C?

What's the naming convention for constants in Objective-C (or most widely used way to name them)? Is there a different criteria for extern constants? Some styles I have seen: NSString* const kPreferenceFirstRun = @"FirstRun"; // Replace "XY" by a…
hpique
  • 119,096
  • 131
  • 338
  • 476
38
votes
4 answers

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

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 extern int i; // Only…
msc
  • 33,420
  • 29
  • 119
  • 214
34
votes
5 answers

Why does "extern const int n;" not work as expected?

My project consists of only two source files: a.cpp: const int n = 8; b.cpp: extern const int n; int main() { // error LNK2001: unresolved external symbol "int const n" (?n@@3HB) int m = n; } I know there are several methods to make it…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
32
votes
6 answers

Reasons to use Static functions and variables in C

I wonder about the use of the static keyword as scope limiting for variables in a file, in C. The standard way to build a C program as I see it is to: have a bunch of c files defining functions and variables, possibly scope limited with…
Gauthier
  • 40,309
  • 11
  • 63
  • 97
32
votes
5 answers

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

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…
Amit Tomar
  • 4,800
  • 6
  • 50
  • 83
32
votes
4 answers

How do I stop name-mangling of my DLL's exported function?

I'm trying to create a DLL that exports a function called "GetName". I'd like other code to be able to call this function without having to know the mangled function name. My header file looks like this: #ifdef __cplusplus #define EXPORT extern…
Slapout
  • 3,759
  • 5
  • 40
  • 61
30
votes
2 answers

Static, extern and inline in Objective-C

What do static, extern and inline (and their combinations) mean in Objetive-C using the LLVM compiler? Also, I noticed that there are CG_EXTERN and CG_INLINE macros. Should we be using those instead? (I couldn't find a source with a clear…
hpique
  • 119,096
  • 131
  • 338
  • 476
29
votes
6 answers

Why can't templates be within extern "C" blocks?

This is a follow-up question to an answer to Is it possible to typedef a pointer-to-extern-“C”-function type within a template? This code fails to compile with g++, Visual C/C++, and Comeau C/C++ with basically the same error message: #include…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
28
votes
10 answers

"Unable to find an entry point named [function] in dll" (c++ to c# type conversion)

I have a dll which comes from a third party, which was written in C++. Here is some information that comes from the dll documentation: //start documentation RECO_DATA{ wchar_t Surname[200]; wchar_t Firstname[200]; } Description: Data structure for…
Evgeny
  • 3,320
  • 7
  • 39
  • 50
26
votes
3 answers

How to make a structure extern and define its typedef

I'm trying to implement tree algorithms in C. I have declared a extern struct in a header file that is completely independent (b_tree_ds.h). Now I plan to import the file in all source files that want to use this struct. So I must declare it using…
simar
  • 544
  • 2
  • 7
  • 15
26
votes
4 answers

Javascript and WebGL, external scripts

Just curious; How do I place my webgl shaders, in an external file? Currently I'm having;