Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:43:36+00:00 2026-06-17T20:43:36+00:00

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

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:43:37+00:00Added an answer on June 17, 2026 at 8:43 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use a formula that is used to work out
Right now I have been trying to use Launchpad's API to write a small
I have been trying to use cx_freeze for some time now, and yet the
I have been trying to use Hibernate for a while. I like hibernate that
I have been trying to use the BigInteger type, that is supposedly new in
I have been trying to use Fixed footer not displaying the bottom-most list item
I have been trying to use Android's AudioManager.setMicrophoneMute() without much success. That is, it
I have been trying to use sql NOW() function while I update a table.
I have been trying to use date_format , however as far as I know,
I have been trying to use array_unique to remove duplicates from the search results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.