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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:08:11+00:00 2026-05-24T16:08:11+00:00

I have three files in my project. a.c b.c test.h test.h declares a namespace

  • 0

I have three files in my project.

a.c
b.c
test.h

test.h declares a

namespace test_namespace {
    int i;
    void f1();
};

test.h is also surrounded by

#ifndef __x
#define __x
...
#endif

now, a.c includes test.h and b.c also includes test.h .
a.c has the main() function, and b.c has the implementation of test_namespace::f1()

However, on compiling this, I get a linking error –

"test_namespace::i is already defined in <b.c's object file mapping in /tmp>"

If I’ve taken care to include conditional compilation preprocessor directives in test.h, why is it being included in both files a.c and b.c ?

Also is noteworthy that if I compile b.c separately as a shared library and then use it as a shared library while linking a.c’s object file, i don’t get this error.

Can someone please explain the above error to me, specially in the face of the conditional compilation directives ?

  • 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-24T16:08:11+00:00Added an answer on May 24, 2026 at 4:08 pm

    Include guards are there to protect multiple header inclusions in the build of a compilation unit. They aren’t necessary for cases when you have two separate code files and one header, like your example.

    (So think more when test.c uses a.h and b.h, in those cases where b.h needs to #include a.h.)

    But that’s a note about what the include guard convention does and how it isn’t buying you anything in this case. The specific technical issue you hit (as others have pointed out) is that you’re basically defining the same variable in two different object files, and when the linker goes to pull everything together it doesn’t know if you want the variable from a.o or b.o.

    ( Note: While compilers can generally be set to override things and build C++ code using features like namespace even if the extension is .c – you probably should be using something else, like .cpp: C++ code file extension? .cc vs .cpp )

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

Sidebar

Related Questions

I have three files in a folder 'test' one.php two.php print.html And i have
I have three files, a main .cpp file: #include <stdio.h> #include myClass.h int main()
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
I have a Unit Test project with 20+ .cs files. I want to run
In my VB.NET project I have three configurations DEBUG|TEST|RELEASE. Currently when I am building
I have unit test project called MyClassTest in TeamTest. This project has three TestMethods.
I have three files: one called sql.php witch has a class db that I
I have three files: movie.h (header); movie.cpp (implementation file); lab9.cpp (driver file using movie
Say I have three files (template_*.txt): template_x.txt template_y.txt template_z.txt I want to copy them
For example, if I have three ASCII files: file1.txt file2.txt file3.txt ...and I wanted

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.