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

  • Home
  • SEARCH
  • 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 8475413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:53:09+00:00 2026-06-10T17:53:09+00:00

I have the world’s easiest program here. I’d imagine it’d only take a second

  • 0

I have the world’s easiest program here. I’d imagine it’d only take a second for some of you to figure out what’s wrong.

foo.h:

#ifndef FOO_H
#define FOO_H

namespace foo
{
    char str[ 20 ];

    void bar(char* s);
}

#endif

foo.cpp:

#include "foo.h"

using namespace std;

namespace foo
{
    void bar(char* s) {
        return;
    }
}

foo_main.cpp:

#include "foo.h"

using namespace std;
using namespace foo;

int main(void)
{
    bar( str );
}

Now when I try to compile these three together:

g++ foo_main.cpp foo.cpp -o foo

/tmp/cc22NZfj.o:(.bss+0x0): multiple definition of `foo::str'
/tmp/ccqMzzmD.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

I want to be using str as a global within the namespace foo, so that needs to be left there. If I move my main method into foo.cpp then things compile fine. What do I do if I want to leave my main method in a separate file? As you can see, I even put include guards on the .h file so that there wouldn’t be a conflict with str, but doesn’t seem to be working. What’s wrong?

  • 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-10T17:53:10+00:00Added an answer on June 10, 2026 at 5:53 pm

    Just like any other global, declare it wherever you need to use it and define it in one place only. So in foo.h, mark it as extern. Then define it in foo.cpp.

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

Sidebar

Related Questions

I have a real world program that is similar to this one, which I'll
I already searched the Web, I have Real World Haskell but I can't figure
I need some help setting up a particular terrain. I have a world that
in Google Documents on some spreadsheets you have that world map widget/plugin that you
I'm a newbie to the Android development world but have some experience with embedded
I have class World which manages creation of object... After creation it calls afterCreation
Does printf occupy memory in the stack? printf(Hello world); Does Hello world have a
Does anyone have a hello world sample or tutorial for creating an Eclipse plugin
In the Java world we have Apache Commons' ToStringBuilder to help with creating toString()
I come from the iPhone development world but have never developed desktop applications. Now

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.