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
11
votes
1 answer

Global variables in Objective-C - difference in extern and top of .m file declaration

I know you can define a global variable in Objective-C by using "extern", but I just realized that the variables I had declared at the top of my .m file before my first method were also accidentally global (and that was causing some problems). I…
Jackson
  • 3,555
  • 3
  • 34
  • 50
11
votes
4 answers

Objective C - How to use extern variables?

I am trying to use extern variables. It complains that because of using numberWithInt I am not passing a contants as the value of my variable So I removed the const and it's complaining that an extern variable must be a constant, so what is the…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
11
votes
1 answer

Explicit instantiation declaration of header only template(extern template)

I am trying to speed up the compile time of the GLM(OpenGL Mathematics). GLM makes heavy usages of C++ templates. This is what I have tried so far. math.h #pragma once #include extern template struct glm::tvec3
hidayat
  • 9,493
  • 13
  • 51
  • 66
11
votes
4 answers

undefined reference when using extern

I have the following setup (hopefully this is not too bare an example): A.h typedef std::map MyClass; extern MyClass inst; A.cpp MyClass inst; B.h #include void foo(); B.cpp #include void foo { …
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
11
votes
4 answers

C2732 - Linkage specification error

I'm using VS2008. I'm getting the following error. BUILD: [02:0000000295:ERRORE] c:\wince700\platform\am33x_bsp\src\bootloader\bootpart\bootpart_e.cpp(61) : error C2732: linkage specification contradicts earlier specification for 'SdhcInitialize'…
Gomu
  • 1,004
  • 5
  • 16
  • 36
11
votes
6 answers

What is the use of Static local variable when we can get a global variable at the same cost?

In C ,what is the use of static storage class when an external variable can serve its purpose at the same cost ie. both occupy storage space in the data segment of the executable. I have much better scope with external variable.If i want the scope…
Adi
  • 729
  • 2
  • 6
  • 13
11
votes
5 answers

extern with global definition of variable in c

I have the following source code which interests me. #include extern int foo; int foo = 32; int main() { printf("%d", foo); } This a perfectly normal piece of code, and when I compile it with gcc -Wall -Wextra -pedantic foo.c I get no…
stdcall
  • 27,613
  • 18
  • 81
  • 125
11
votes
3 answers

extern const char* const SOME_CONSTANT giving me linker errors

I want to provide a string constant in an API like so: extern const char* const SOME_CONSTANT; But if I define it in my static library source file as const char* const SOME_CONSTANT = "test"; I'm getting linker errors when linking against that…
AndiDog
  • 68,631
  • 21
  • 159
  • 205
11
votes
1 answer

Redefinition; different basic types (typedef struct)

I'm having a bit of trouble trying to get structs to work properly when they are defined in different files. From as far as I can tell, the error is telling me that the struct is being defined two different times. I believe that perhaps I may need…
Tundra Fizz
  • 473
  • 3
  • 10
  • 25
10
votes
7 answers

How to declare extern 2d-array in header?

We have this declaration in LCD.c: unsigned char LCD[8][64] = {((unsigned char) 0)}; And in LCD.h we want to have something like: extern unsigned char LCD[][]; We get this error: Error[Pe098]: an array may not have elements of this type
user1106072
  • 331
  • 2
  • 4
  • 8
10
votes
3 answers

C++ global extern "C" friend can't reach private member on namespaced class

Please consider the code: #include using namespace std; extern "C" void foo( void ); namespace A { template< int No > class Bar { private: friend void ::foo( void ); static void…
j4x
  • 3,595
  • 3
  • 33
  • 64
10
votes
5 answers

Is it possible/safe/sane to pass around a function pointer to a static function?

Let’s say I only want to expose a function from one of my files by passing out a function pointer to that function. Is it safe to declare that function as static? Are compilers allowed to do any judo that would invalidate my function pointer, or…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
10
votes
2 answers

Difference extern"C" vs extern

Is there a difference whether I use the extern "C" specifier for the entire header, or specify extern for every function? As far as I know, there is none, since only functions and variables can be linked externally, so when I use the extern…
user7747610
10
votes
4 answers

Passing a struct to a template with extern const. What is the extern for?

I am asking myself why the following code works and what the specifier extern does when instantiating baz_instance: struct baz { int value; }; extern const baz baz_instance = {3}; template int foo(){ return b.value; } int…
smoes
  • 591
  • 2
  • 17
10
votes
1 answer

How does extern work in namespaces?

I'm running a simple program similar to what I found here. It's meant to reduce code bloat when including constants in multiple files. It does this by using const global variables within a namespace with their respective extern forward…
Wandering Fool
  • 2,170
  • 3
  • 18
  • 48