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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:31:04+00:00 2026-05-24T09:31:04+00:00

I know this has been asked a billion times before, but I’m still having

  • 0

I know this has been asked a billion times before, but I’m still having trouble.

I began with one main.cpp file that contained all of my code. Say it looked like this:

int a = 0;
void foo() {
    a+1;
}

void bar() {
    a+2;
}

int main() {
    foo();
    bar();
    a + 3;
}

Now I want to split up this code into multiple files for easier management. I would like to only have one header, header.h, and three .cpp files: main.cpp, foo.cpp, and bar.cpp.

ATM, this is what I have:

//header.h
int a = 0;
void foo();
void bar();

.

//foo.cpp
#include "header.h"
void foo() {a+1;}

.

//bar.cpp
#include "header.h"
void bar() {a+2;}

.

//main.cpp
#include "header.h"
int main() {
    foo();
    bar();
    a + 3;
}

Unfortunately, the linker has been complaining that I’ve defined a multiple times. I’ve tried using #ifdef, but that only guards against redefining in the same file, correct? How can I make this work?

EDIT: Modified the question, I just realized that it is the variables that have been defined multiple times, not the functions.

  • 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-24T09:31:05+00:00Added an answer on May 24, 2026 at 9:31 am

    I think the bigger problem is you are implementing something in a header file. The problem is when you include the header into more than one object file, you will run into problem at linker time. Don’t do it.

    Use a define for this sort of thing. They difference is that a define, being a preprocessor macro, doesn’t create any object code by itself, and won’t create any symbols. There are advantages to declaring some sort of const type instead of a define, but I think not having to worry about symbol collisions at link time is a bigger benefit.

    Your header file needs include guards, as well

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

Sidebar

Related Questions

I know this has been asked many times before, but i still can't seem
I know this has been asked 100 times before but every bit of code
I know this has been asked a few times before, but the existing solutions
I know this has been asked several times before but I can't seem to
I know this has been asked a couple of times before, but not of
I know this has been asked before, but I'm still undecided on which PDF
I know this has been asked different ways several times, but I'm just not
I know this has been asked thousands of times but I just can't find
I know this has been asked many times but i'm yet to find a
I know this has been asked before but all the answers I found didn't

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.