Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern variable causes multiple definition error

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)
like image 443
abumusamq Avatar asked Jan 26 '13 06:01

abumusamq


1 Answers

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.

like image 57
Alok Save Avatar answered Oct 11 '22 23:10

Alok Save