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 246857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:12:27+00:00 2026-05-11T21:12:27+00:00

If I declare a global variable in a header file and include it in

  • 0

If I declare a global variable in a header file and include it in two .cpp files, the linker gives an error saying the symbol is multiply defined.
My question is, why does this happen for only certain types of object (eg. int) and not others (eg. enum)?

The test code I used is given below:

test.h

#ifndef TEST_HEADER
#define TEST_HEADER

namespace test
{           
        int i_Test1 = -1;
        int i_Test2 = -1;
};

#endif // TEST_HEADER

class1.h

#ifndef CLASS_1_HEADER
#define CLASS_1_HEADER

class class1
{
public:
        void count();
};

#endif //CLASS_1_HEADER

class1.cpp

#include <iostream>
#include "class1.h"
#include "test.h"

void class1::count()
{
        std::cout << test::i_Test1 << std::endl;
}

class2.h

#ifndef CLASS_2_HEADER
#define CLASS_2_HEADER

class class2
{
public:
        void count();
};

#endif //CLASS_2_HEADER

class2.cpp

#include "class2.h"
#include <iostream>
#include "test.h"

void class2::count()
{
        std::cout << test::i_Test2 << std::endl;
}

main.cpp

#include "class1.h"
#include "class2.h"

int main(int argc, char** argv)
{
        class1 c1;
        class2 c2;
        c1.count();
        c2.count();
        return -1;
}

Building this code with:

g++ main.cpp class1.cpp class2.cpp -o a

produces the following output:

ld: fatal: symbol test::i_Test1' is
multiply-defined:
(file /var/tmp//ccwWLyrM.o type=OBJT; file /var/tmp//ccOemftz.o
type=OBJT); ld: fatal: symbol
test::i_Test2′ is multiply-defined:
(file /var/tmp//ccwWLyrM.o type=OBJT; file /var/tmp//ccOemftz.o
type=OBJT); ld: fatal: File processing
errors. No output written to a
collect2: ld returned 1 exit status

If I change the test.h file as given below:

test.h (with enum)

#ifndef TEST_HEADER
#define TEST_HEADER

namespace test
{
        enum val
        {
                i_Test1 = 5,
                i_Test2
        };
        //int i_Test1 = -1;
        //int i_Test2 = -1;
};

#endif // TEST_HEADER

I don’t get the “multiply defined” error and the program gives the desired output:

5
6
  • 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-05-11T21:12:27+00:00Added an answer on May 11, 2026 at 9:12 pm

    That’s because enumerations are not objects – they are types. Class types (class,struct,union) and enumerations can be defined multiple times throughout the program, provided all definitions satisfy some restrictions (summed up by the so-called One Definition Rule (ODR)). The two most important ones are

    • All definitions have the same token sequence (textual identical)
    • Names used must have the same meaning (resolve to the same things) in all definitions. (this is a requirement on the context of the definition)

    Your enumeration definition satisfies all conditions of the ODR. Therefor, that is valid and no reason for the linker / compiler to moan (actually, for a violation of the ODR the compiler is not required to issue a message either – most of it falls under the so-called no diagnostic required rule, some violations also result in undefined behavior).

    However, for every non-inline function and object, these must be only defined one time. Multiply defining those result in spurious errors, like in your case. To solve it, put only a declaration into the header file (using “extern” without an initializer) and put one definition into one of those .cpp files (omitting the “extern” then, or putting an initializer. If it is a const object, you still need the “extern”, since per default const variables have internal linkage, and the symbol would not be exported otherwise).

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

Sidebar

Ask A Question

Stats

  • Questions 167k
  • Answers 167k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using OpenSSL.NET will make no-padding available. However, I can't make… May 12, 2026 at 1:30 pm
  • Editorial Team
    Editorial Team added an answer Apparently there was a memory issue. The nightly backup would… May 12, 2026 at 1:30 pm
  • Editorial Team
    Editorial Team added an answer Firstly, you can easily simplify that code: @Override public void… May 12, 2026 at 1:30 pm

Related Questions

I'm having some trouble compiling/linking a set of classes, several of them dealing with
I have a site built in Ruby On Rails which has many ads in
What's the difference between: function bar() { for (x=0; x< 100; x++) {} }
I'm working on some VBA scripts for an Excel worksheet, and I've created a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.