0

First of all I know this isnt "correct." However, I like to test things out and I have run into this problem that if I create a global variable in the header file and declare it extern in the main.cpp file, I can use it(Note that I did not include the class header on this example). However, if I actually try to do the same thing, the only difference being including the class header, I get an error.

(error: ld returned 1 exit status).

I wonder why this happens?

Code as requested:

Main.cpp:

#include <iostream>
#include "albino.h"
using namespace std;

extern int iVar;

int main()
{
    cout << iVar << endl;
}

albino.h:

#ifndef ALBINO_H
#define ALBINO_H

int iVar = 10;

class albino
{
    public:
        albino();
};

#endif // ALBINO_H

The albino.cpp doesnt have anything.

ERROR: ||error: ld returned 1 exit status|

J. Allan
  • 1,418
  • 1
  • 12
  • 23
  • 1
    Include your code and the complete error message in the question please. – Richard Critten Aug 20 '16 at 00:52
  • Why not post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)? Why not create a gloval vairable in a source file and declare it extern on the header file? – MikeCAT Aug 20 '16 at 00:53
  • @RichardCritten done. –  Aug 20 '16 at 00:58
  • @MikeCAT Because I am testing things out to see how they work.Im aware that there are workarrounds that are way more correct. –  Aug 20 '16 at 00:59
  • 1
    `extern int iVar;` is a declaration. `int iVar = 10;` is a definition. Make sure you understand the difference. – IInspectable Aug 20 '16 at 01:20
  • 1
    Possible duplicate of [How to correctly use the extern keyword in C](http://stackoverflow.com/questions/496448/how-to-correctly-use-the-extern-keyword-in-c) and [How do I use extern to share variables between source files in C?](http://stackoverflow.com/q/1433204/1889329). – IInspectable Aug 20 '16 at 01:24
  • As a beginner just stay away from globals, they're Evil™. When you get more experience, where you need a global state preferentially use a singleton. Note that much use of singletons indicates a design problem. – Cheers and hth. - Alf Aug 20 '16 at 04:32
  • The above code I have run on my linux system 32bit, gcc 4.4.7 and the output was come as expected, I don't find any error here. – Arun Pal Nov 21 '17 at 12:45

1 Answers1

2

I think you are doing it the wrong way around.

You can define a global variable only once; but you can declare it many times, wherever you want to use it.

That means int i = 0; should only be existing once, so _not in the header, but in exactly one cpp file (doesn't matter for the compiler which one, only for humans that try to find it); and extern int i; can be in the header so it is repeated everywhere. See also How do I use extern to share variables between source files?

Community
  • 1
  • 1
Aganju
  • 6,295
  • 1
  • 12
  • 23
  • I edited the question,I am only defining the global variable once but when I include the class header on main.cpp it doesnt work,but with works without the include.I am aware that I should not declare global variables on the header file –  Aug 20 '16 at 01:01
  • @cobzz: This answer is still correct. You say you are aware that you should not declare global variables in the header, yet that's what you're doing. (the word is actually "define" and the difference is key) – Lightness Races in Orbit Aug 20 '16 at 01:15
  • The thing is i want the "technical" explination on why I cant do that.I know I shouldnt, but why exactly? –  Aug 20 '16 at 02:00
  • @cobzz: If you define the variable in the header, then only one file that includes the header can be linked into any program — because if you have more than one file that includes the header, you have multiple definitions of the variable. Since the reason for creating a header is to use it in more than one file (if it's only needed in one file, there's no need for a separate header), making the 'shareable' header such that it cannot be shared defeats the object of the exercise. Or, in other words, defining (as opposed to declaring) global variables in headers is basically always wrong. – Jonathan Leffler Aug 20 '16 at 02:31