-7

I'm just testing out how extern works (using MSVC) and I cannot get it to work, no matter what I do:

// Test.h
int externalint = 10

// Main.cpp
void main()
{
  extern int externalint;
  std::cout << externalint << std::endl;
  std::cin.ignore();
}

This results in a linking error, even though I defined it in the header. I do not wish to include the header because the way I read it says it can be in another translation unit and does not need to be included. Am I wrong with the way I am reading it or did I write something incorrectly? If I include the header, it works, as it should, even without the extern declaration.

Krystian S
  • 1,586
  • 7
  • 23

3 Answers3

6

Header files are usually not translation units but meant to be included by them. That's why header files usually do not "define" variables, since this would lead to multiple definition errors when the header file is included by different translation units (thereby re-defining the variable again and again).

That's where "extern" comes into place, since this is for just "declaring" a variable without "defining" it. "extern" means "will be defined in some other translation unit".

So the usual way is:

// Test.h
extern int externalint;  // just declares externalint

// Test.cpp
int externalint = 10;  // defines externalint

// main.cpp
#include "Test.h"  // importing the declaration of "externalint" defined elsewhere
int main() {
  std::cout << externalint << std::endl;  
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
2

Header files are not typically translation units.

Put the definition in a Test.cpp file.

int externalInteger = 10;

Put your main function in Main.cpp file.

#include <iostream>

int main()
{
  extern int externalInteger;
  std::cout << externalInteger << std::endl;
  std::cin.ignore();
}

Compile and link.

g++ -c Test.cpp
g++ -c Main.cpp
g++ Main.o Test.o

For MS, use cl.exe as the compiler instead of g++.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

First ask yourself why would you use such a thing as extern. And then you will understand how to use it. Imagine you would like a piece of code to be called in different programs. Once from console application and once from an external program.

So you will write Test.cpp.

Now you can use it at console app using main() function and once you build it together with the external program you can use it there too.

Both will use the externalint value of 10 and as long as you update this Test library in both projects like through git. You would have the identical behaviour in both the console application and the external program.

Where is the Test.h you ask? There you go:

The Test.cpp will be compiled separately from your Main.cpp but you would like to use the variables and functions from your Test.cpp. How does the compiler know what functions are there?

You will let him know. You simply create an interface -> Test.h. And then you will include it in your Main.cpp

#include "Test.h"

Example of interface:

//Test.h
int externalint = 10;

But, what if you have more .cpp files interested in this interface? Then it will be duplicate and it will not compile. Because you cannot initialize the same variable twice.

Luckily you can declare it and announce that the initialization will be done in one of the .cpp files.

// Test.h
extern int externint;
// Test.cpp
int externint = 10;
Volt
  • 99
  • 1
  • 7