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

How to use switch with extern constants?

Some code.cpp file contains extern const int v1; extern const int v2; extern const int v3; extern const int v4; int _tmain(int argc, _TCHAR* argv[]) { int aee = v1; switch (aee) { case v1: break; case v2: …
Unicorn
  • 1,397
  • 1
  • 15
  • 24
12
votes
1 answer

What is the purpose of external static constructors in C#?

Accordingly to the section "10.12 Static constructors" of "C# Language Specification. Version 5.0" static constructor can be marked with "extern" modifier and in this case it's said to be an external static constructor. The ordinary (non-external)…
12
votes
3 answers

How can I use external variables in Python like 'extern int x;' in C?

How can I use external variables in Python, like extern int x; in C? For example, main1.py: from myfunc import print_a a = 10 print a print_a() myfunc.py: def print_a(): global a print a
Je-Hoon Song
  • 301
  • 1
  • 4
  • 7
12
votes
1 answer

In C, why don't I get an error when I declare an global variable in different data type in an other file?

I have tried the following code: File1.c: int x; File2.c: extern char x; main() { x=10; .... .... } and compiled as $gcc File1.c File2.c and I didn't get any error but I was expecting one.
Adi
  • 729
  • 2
  • 6
  • 13
12
votes
3 answers

How can I implement my own type of extern?

In our product, we have things called "services" which are the basic means of communication between different parts of the product (and especially between languages—an in-house language, C, Python and .NET). At present, code is like this…
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
12
votes
4 answers

How does the linker know where is the definition of an extern function?

I read a few posts and concluded that extern tells compiler that "This function exists, but the code for it is somewhere else. Don't panic." But how does the linker know where the function is defined. My CASE:- I am working on Keil uvision 4. There…
Ankit Gupta
  • 185
  • 1
  • 6
  • 16
12
votes
5 answers

extern C can not be used at class level?

Just want to confirm in Windows environment, VSTS 2008 + C++ project, we could only apply extern C to function level, not be able to apply to class level (so that all member functions from the class use C language name mangling)? I have tried…
George2
  • 44,761
  • 110
  • 317
  • 455
12
votes
5 answers

extern declaration and function definition both in the same file

I was just browsing through gcc source files. In gcc.c, I found something like extern int main (int, char **); int main (int argc, char **argv) { Now my doubt is extern is to tell the compiler that the particular function is not in this file but…
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
11
votes
2 answers

No linkage at block scope?

Do all variables declared in a block have 'no linkage'? For example: 1: If I declare a static variable: void foo() { static int i; } Would it have an internal linkage or no linkage? If no linkage, then why make it static? 2: What happens if I…
user103214
  • 3,478
  • 6
  • 26
  • 37
11
votes
8 answers

C++ best way to define cross-file constants

I am working on a game and have an interesting question. I have some game-wide constant values that I want to implement in one file. Right now I have something like this: constants.cpp extern const int BEGINNING_HEALTH = 10; extern const int…
rlbond
  • 65,341
  • 56
  • 178
  • 228
11
votes
1 answer

C++/CLI->C# error C2526: C linkage function cannot return C++ class

I have a simple .NET dll built with VS2010 C# that exposes 2 static members of a class public class Polygon { public static void Test(int test) {} public static void Test(List test) {} } I then created a Console app from VS2010 C++ and…
PeskyGnat
  • 2,454
  • 19
  • 22
11
votes
1 answer

Using `extern template` with third-party header-only library

I am using the glm library, which is a header-only collection of math utilities intended for 3D graphics. By using -ftime-trace on Clang and ClangBuildAnalyzer, I've noticed that a lot of time is being spent instantiating glm types: **** Templates…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
11
votes
1 answer

Why same named extern local variables in different blocks get different linkages between compilers in c++?

While I was just checking which linkages are granted to extern local variables I found that some different behavior between compilers for instance if I tested below code as you see in the comments variable vars have different linkages // foo.cpp int…
hyuk myeong
  • 197
  • 1
  • 13
11
votes
2 answers

extern enum in c++

I have an enum I have declared in some .h file: typedef enum { NONE, ONE, TWO, THREE } MYENUM; in a seperate .cpp I cannot do this: extern enum MYENUM; //works extern MYENUM TWO; //makes sence, TWO is not an INSTANCE of…
Tom Fobear
  • 6,729
  • 7
  • 42
  • 74
11
votes
2 answers

Different compilation results not using extern in C vs in C++

When I declare a global variable in two different source files and only define it in one of the source files, I get different results compiling for C++ than for C. See the following example: main.c #include #include "func.h" // only…
Mike van Dyke
  • 2,724
  • 3
  • 16
  • 31