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

pass c++ function that contains struct to C# and convert it to c# function with Marshal.PtrToStructure

I am very new to c++ so I might have some mistakes on this side. So I started with writing simple C++ function that will contain struct as return type: my c++ struct: struct a { int i; }; my c++ function declaration in library.h file: extern "C"…
inside
  • 3,047
  • 10
  • 49
  • 75
-1
votes
2 answers

linking the "C" style code in c++

I am trying to run C style code in C++ and my compiler is giving the following error: Line 5: error: expected unqualified-id before string constant Second: my goal is to understand the error at "line d". using namespace std; typedef int…
dead programmer
  • 4,223
  • 9
  • 46
  • 77
-1
votes
1 answer

return makes pointer from integer without a cast - but types are okay?

Just some code. In one file and its header: // header: extern char * thessc_config_member_get_config_key(struct config_member_t *m); // implementation: inline char * thessc_config_member_get_config_key(struct config_member_t *m) { return…
musicmatze
  • 4,124
  • 7
  • 33
  • 48
-1
votes
1 answer

undefined symbol of a struct pointer

I'm working on xinu, and I need to change some *.c files. I have this struct in file ready.c: struct newtimer{ struct newtimer* tnext; struct newtimer* tprev; int tkey; int tprio; int tcount; }; and then I declared: struct…
Alaa M.
  • 4,961
  • 10
  • 54
  • 95
-1
votes
2 answers

How to declare/use extern map in c++

I'm not very used and don't really understand the use of global variables in c++, and cannot manage to solve a problem here, although I'm nearly sure I'm using the declarations the right way. I've got an unordered map, which contains params from a…
François Laenen
  • 171
  • 4
  • 14
-2
votes
1 answer

Strange things in C/C++ code (ISO C++ forbids declaration of 'xxx' with no type)

I am a C/C++ newbie, so this might be a dumb question, I have the following problem, I have a method in a c++ lib which is exported using extern 'C' and it is being called by another method from a different c file. So, I needed some structures in…
Tancho
  • 1,581
  • 3
  • 22
  • 40
-2
votes
1 answer

How does '#include' actually work in c++?

Suppose that I have 2 files: header.h and main.cpp (they are all in the same folder) -header.h int x; -main.cpp #include "header.h" #include extern int x; int main(){ x=1; std::cout<
vdung07
  • 1
  • 1
-2
votes
1 answer

C++ Trying to write a method that changes a value in an extern vector

so I have three files here that are important: main.cpp, common.h, EngineScript.cpp. I am declaring my multidimensional vector in common as extern vector> PositionVector(vector); I am defining it in main.cpp with …
setanta
  • 11
  • 3
-2
votes
1 answer

./maincpp.h(4): error: expected identifier or '('

#ifndef MAINCPP_H #define MAINCPP_H extern "C" { void displaylistfun(void); void strfun(void); } #endif Don't know why i am getting this error, I am trying to call Cpp function in .c file
Sana Mev
  • 111
  • 8
-2
votes
3 answers

Is it possible to use a static variable declared in another C++ library

I intend to use a library with a declaration of a variable in one of its function /// in library A function fun(){ static int iwanttouse = 1; /// operation on iwanttouse } How can i use it in Application B? Do I connect it with…
user1538798
  • 1,075
  • 3
  • 17
  • 42
-2
votes
1 answer

Non-Static Extern variable

I am initializing an extern variable in a herder file and then use it in (.c) file but when i compile my code i get an warning that says: no previous extern declaration for non-static variable. Here is my code: enter code here /*led.h extern int…
-2
votes
1 answer

Already defined in .obj even when using extern and #ifdef

I have three files like this: head.h #ifndef HEAD_ #define HEAD_ extern int f(); #endif mycpp.cpp #include "head.h" int f() { return 5; } myMain.cpp #include #include "head.h" #include "mycpp.cpp" int main() { std::cout <<…
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58
-2
votes
1 answer

Can 'extern' be omitted when using global variables?

Recently I found a strange thing about 'extern' in C When I compile it using gcc tmp.c tmp2.c -o tmp -Wall, it turns out pretty well with no compile errors (nor warnings!). The output is 1. But the following code won't pass the linkage process (the…
Hans
  • 27
  • 5
-2
votes
2 answers

c extern undefined reference

I'm sorry for what I'm sure is a simple mistake. But after a few hours I can't figure out what I'm doing wrong. I understand that extern needs to be declared outside a function and defined within a function. But I can't get it to work. Here is my…
Akim
  • 147
  • 1
  • 8
-2
votes
1 answer

Why is `extern` used in this MPLAB C example?

In the MPLAB XC8 Compiler User Guide, an example on page 162 (reproduced below) uses the extern keyword in conjunction with the @ specifier. Given that we are specifying the address ourselves, why is this needed? It's not going to be allocating any…
Sod Almighty
  • 1,768
  • 1
  • 16
  • 29