0

I'm developping a Blackberry 10 mobile app. using the momentics IDE (BB Native SDK).

In my application, I want to use global variables that will be shared by many classes.

I tried the code below like described in this link, but when I add the extern instruction before the declaration of the variable "g_nValue* " in the ".h" file, it returns the error "storage class specified for 'g_nValue'"

*/ global.cpp:

// declaration of g_nValue
int g_nValue = 5;

*/ global.h:

#ifndef GLOBAL_H // header guards
#define GLOBAL_H

// extern tells the compiler this variable is declared elsewhere
extern int g_nValue;

#endif

Any one have an idea on this? I searched a lot and they all said that the extern instruction should not cause any trouble.

Mohamed Jihed Jaouadi
  • 1,427
  • 4
  • 25
  • 44
  • 3
    The code looks correct. Googling about this error, it probably looks like [you have a syntax error elsewhere in the file](http://stackoverflow.com/a/17381614/1600898). – user4815162342 Dec 06 '13 at 10:13
  • 1
    Is that your whole `global.h` file? What is included just before `global.h` is included? I suspect there's a missing semicolon. – Michael Burr Dec 06 '13 at 10:19

4 Answers4

1

An alternative to extern are static variables inside a class:

//.h
struct Globals
{
    static int g_global_var;
};

//.cpp
int Globals::g_global_var = 0;

//usage:
Globals::g_global_var;
Raxvan
  • 6,257
  • 2
  • 25
  • 46
  • the static members are not global in C++, each .cpp file will has its own copy of the variable like explained in this [forum](http://stackoverflow.com/questions/14349877/static-global-variables-in-c) instead of using the global variable – Mohamed Jihed Jaouadi Dec 06 '13 at 10:06
  • 4
    @user2072762 incorrect. `static` at namespace scope is different to `static` at class scope. – Simple Dec 06 '13 at 10:13
  • As Simple says. I can't remember I ever encountered a valid use case for `extern` in a modern C++ codebase. – Frank Osterfeld Dec 06 '13 at 13:13
1

the extern qualifier only tells the compiler, "this symbol is defined in a different source file" - so the symbol exists, it's safe to use it. You will get a linking error if you actually "lie" about it and don't define the symbol - but that's a different story.

There does not seam to be any problem with the code you showed us. But here is a link which might help you get a better idea...

Pandrei
  • 4,843
  • 3
  • 27
  • 44
  • Perhaps you could make a small 'recap' of what the link contains of info - we do not know how long the link will work, and your answer is connected to information inside this other website – serup Oct 10 '16 at 08:12
0

You don't declare the variable exteren in the compilation unit it's defined in. You only declare it extern (and don't define it) if you want to use it in other .cpp files (compilation units).

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Sorry !! but I didn't understand your answer. Can you explain more ? – Mohamed Jihed Jaouadi Dec 06 '13 at 10:03
  • That is exactly what the OP is doing: *declaring* the variable with use of `extern` (in the header), and *defining* it without `extern`. – user4815162342 Dec 06 '13 at 10:13
  • 1
    It's perfectly fine to have an `extern` declaration for a variable in the same translation unit as the definition for the variable. In fact, I think it's a good practice to have the file that defines the variable also include the header that declares it `extern` so you can be sure the type in the header's declaration matches with the actual definition. – Michael Burr Dec 06 '13 at 10:17
0

Your code seems fine. Maybe the you have an error elsewhere. Maybe there is a semicolon (;) missing in a line before extern int g_nValue.

rozina
  • 4,120
  • 27
  • 49