5

I have been trying to use extern in order to use variable that is previously defined.

I have not used extern before and now I need to use it in order to define variable just once and use them across multiple files

I have written minimized version of code for this question. I have four files

lib.h

#ifndef LIB_H
#define LIB_H

#include <iostream>

namespace lib {

  extern bool initialized;

  bool initialized = false;

  static void isInit(char* parent) {
    std::cout << "Library for [" << parent << "] initialized? " << (::lib::initialized ? "yes" : "no") << "\n";
  }
} // namespace lib
#endif

vehicle.h

#ifndef _VEHICLE_H
#define _VEHICLE_H
#include <string>

class Vehicle {
  public:
    Vehicle(const std::string& manufacturer,
            const std::string& model,
            int year);
    std::string manufacturer;
    std::string model;
    int year; 
};
#endif

Following is implementation of vehicle.h file called vehicle.cpp

#include "vehicle.h"

#include "lib.h"

Vehicle::Vehicle(const std::string& manufacturer,
                 const std::string& model,
                 int year) :
                    manufacturer(manufacturer),
                    model(model),
                    year(year) {
   ::lib::isInit("Vehicle");
}

main.cpp

#include "vehicle.h"

#include "lib.h"

int main(int argc, char** argv) {

   ::lib::isInit("main");

   ::lib::initialized = true;

   ::lib::isInit("main");

   Vehicle vehicle("Toyota", "Corolla", 2013);

   return 0;
}

I am using g++

g++ -Wno-write-strings main.cpp vehicle.cpp -o bin/main.cpp.bin 

I get following errors:

/tmp/cclVpsgT.o:(.bss+0x0): multiple definition of `lib::initialized'
/tmp/ccmJKImL.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status

I have checked the output of:

g++ -Wno-write-strings main.cpp vehicle.cpp -E

multiple definition occurs every time lib.h is included.

My questions are:

  • Why is lib.h included multiple times when define guard is there
  • How would I define 'extern' variable and initialize it in the same file (since it's used in the same file later)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
abumusamq
  • 780
  • 1
  • 10
  • 29

1 Answers1

8

Why is lib.h included multiple times when define guard is there

You need to remove the definition:

bool initialized = false;

And put it in one and only one source file.

Include guards prevent the same header file from getting included more than once in the same translation unit(TU) not in different translation units.
You define the variable initialized in header file which gets included across different translation units and then each TU has a symbol named initialized which breaks the one definition rule.

How would I define 'extern' variable and initialize it in the same file (since it's used in the same file later)

If you want the variable to be used in the same file, why make it extern? You need to use extern when you want to share the same variable accross different TUs.
If you need to use it at global scope in only single TU, You should simple put it inside a unnamed namespace.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • is there no way to do it in the same file? – abumusamq Jan 26 '13 at 06:41
  • @mkhan3189: Does answer to the second question answer the Q in your comment? – Alok Save Jan 26 '13 at 06:44
  • Yes it does :) marking this as answer since this does answer my question. I believe there is no way to initialize in the same file since it's extern. – abumusamq Jan 26 '13 at 06:50
  • but before I mark this as answer, do you have any idea on how would I use the same variable to be defined and declared in the same file (of course extern needs at least two files to use this and static makes its own copy of variables for each include) but is there any other way? (hope question is clear) – abumusamq Jan 26 '13 at 06:52
  • @mkhan3189: It is not possible because you cannot define a object in header file and include this header in multiple files. It violates ODR. – Alok Save Jan 26 '13 at 06:52