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
9
votes
5 answers

How to call a C++ method from C?

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…
totten
  • 2,769
  • 3
  • 27
  • 41
9
votes
1 answer

Is there a way to do an `extern alias` inside a razor (MVC3) view?

Is there a way to do an extern alias inside a razor (MVC3) view? I have two versions of the same assembly (i.e. 1.0 and 2.0) with a type that has the same name and namespace and I need a way to specify the newer one in the razor view. I've…
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97
8
votes
1 answer

How to link two nasm source files

I've got a file that defines very basic IO functions and I want to create another file that uses this file. Is there a way to link these two files up? prints.asm: os_return: ;some code to return to os print_AnInt: ;some code to output an…
meltuhamy
  • 3,293
  • 4
  • 23
  • 22
8
votes
4 answers

extern C and struct method

Given the following C++ code, #ifdef __cplusplus extern "C" { #endif struct foo { void getNum() { } }; #ifdef __cplusplus } #endif int main (int argc, char * const argv[]) { return 0 ; } Is…
user195488
8
votes
3 answers

What are the requirements for C++ template parameters?

If you are using a template in C++ that takes an integer value as a parameter, are there any requirements on an integer variable used as the parameter that are different than if the variable was used as a parameter in a function call? This is a…
jeffD
  • 1,230
  • 3
  • 13
  • 18
8
votes
1 answer

extern "C" Default argument works or not?

From Here it seems that default argument are not supported by C. I have the following method in exported library: extern "C" { __declspec (dllexport) uintptr_t Method(int freq, int *pRetval, bool *support2MHz); } If i made last argument…
Erik Šťastný
  • 1,487
  • 1
  • 15
  • 41
8
votes
1 answer

Safe to pass empty variables by value, when they have no definition?

If I pass an empty variable by value, even though it has no definition, is it safe and compliant? I came across this issue while working on code, overloading |, to make this print the contents of the vector v: v | print; // prints the vector v The…
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
8
votes
1 answer

Function templates: extern template vs explicit specialisation

Consider the following function template declaration: template >> void foo(T i); There is only one possible valid instantiation of this template, namely with T = int. I'd like to put…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
8
votes
3 answers

Why would you use `extern void my_func();` rather than including `my_utils.h`?

I'm working on some code I didn't write and noticed that there are many extern void my_func();. My understanding is that extern in for global variables, not for functions. Is there a practical reason to declare a function as extern rather than…
coolaj86
  • 74,004
  • 20
  • 105
  • 125
8
votes
2 answers

How to correctly write declarations of extern arrays (and double arrays) in C's header files?

Suppose I want to share a global array of data across my program, for example: int lookup_indexes[] = { -1, 1, 1, -1, 2, 1, 1, -2, 2, 2, -1, 1, 1, 2 }; What is the correct extern declaration for this array in the C header file? Also what about an…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
8
votes
3 answers

extern declaration, T* v/s T[]

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…
Gyapti Jain
  • 4,056
  • 20
  • 40
8
votes
2 answers

Why does this separate definition cause an error?

Challenge: I have this code that fails to compile. Can you figure out what's wrong? It caused headache to me once. // header namespace values { extern std::string address; extern int port; } // .cpp file std::string ::values::address =…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
8
votes
2 answers

What's the difference between `extern int (x)[]` and `extern int x[]` in C?

There is declaration extern int (x)[] at the end of this article. Are the parentheses doing anything or they are just for confusion? My guess would be that with parentheses x is an array of external integers (and that's what the article says), but…
Džuris
  • 2,115
  • 3
  • 27
  • 55
8
votes
2 answers

c99 inline semantics with gcc (mspgcc)

I am writing a couple of functions that I would like to inline. Reading here and using the second c99 inline option with inline on all declarations and definitions, like so: extern inline void SPFD54124B_write_cmd(uint16_t command); in a header,…
evading
  • 3,032
  • 6
  • 37
  • 57
8
votes
5 answers

An extern variable located in a function?

According to wikipedia: http://en.wikipedia.org/wiki/External_variable An external variable may also be declared inside a function. What is the purpose of an extern variable being declared within a function? Would it have to be static too?
user997112
  • 29,025
  • 43
  • 182
  • 361