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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:31:53+00:00 2026-06-09T07:31:53+00:00

I have problem with multiply declaration in c++, but not in c. You could

  • 0

I have problem with multiply declaration in c++, but not in c.
You could see code for more information.

file main.c

#ifndef VAR
#define VAR
int var;
#endif
int main(){}

file other.c

#ifndef VAR
#define VAR
int var;
#endif

Compile with gcc

gcc main.c other.c
>> success

Compile with g++

g++ main.c other.c
Output:
/tmp/ccbd0ACf.o:(.bss+0x0): multiple definition of `var'
/tmp/cc8dweC0.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

My gcc and g++ version:

gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • 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-09T07:31:57+00:00Added an answer on June 9, 2026 at 7:31 am

    Your code is formally incorrect in both C and C++ due to multiple definitions of variable var. It is just that this type of error was traditionally overlooked by C compilers as a popular non-standard extension. This extension is even mentioned in C language specification

    J.5 Common extensions

    The following extensions are widely used in many systems, but are not portable to all
    implementations. […]

    J.5.11 Multiple external definitions

    There may be more than one external definition for the identifier of an object, with or
    without the explicit use of the keyword extern; if the definitions disagree, or more than
    one is initialized, the behavior is undefined (6.9.2).

    But formally, you have absolutely the same multiple-definition error in both C and C++ languages. Ask your C compiler to behave more pedantically (disable extensions, if it has an option for that) and your C compiler shall also generate the very same error as your C++ compiler.

    Again, you code contains multiple definitions of variable var, which is an error in both C and C++. Your #ifdef directives do not solve anything at all. Preperocessor directives cannot help you here. Preprocessor works locally and independently in each translation unit. It can’t see across translation units.

    If you want to create a global variable (i.e. the same variable shared by all translation units), you need to make one and only one definition of that variable

    int var;
    

    in one and only one translation unit. All other translation units should receive non-defining declarations of var

    extern int var;
    

    The latter is typically placed in a header file.

    If you need an individual, independent variable var in each translation unit, simply define it in each translation unit as

    static int var;
    

    (although in C++ this usage of static is now deprecated and superseded by nameless namespaces).

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

Sidebar

Related Questions

I want to implement Hill cipher but I think I have a problem in
i have a problem calling multiple instance of a class that i have coded
I have a problem conserning uploading multiple files to my ftp server and I
I have segmentation fault problem with my multiple class files project. Without making empty
i have a problem with triggering the multiple alarm at first time , here
i have been reading about multiple inheritance What is the exact problem with multiple
I have a problem with my site. The site has multiple entities: Articles, Posts,
I am building multiple forms app in Builder XE2 and I have a problem
I have a simple problem while putting text in multiple table! please have a
The problem is this: I have multiple competing threads (100+) that need to access

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.