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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:57:21+00:00 2026-06-13T01:57:21+00:00

I’ve written a simple program in C++ using OpenGL. What I need is to

  • 0

I’ve written a simple program in C++ using OpenGL. What I need is to have couple global variables in two files – main.cpp and funcs.cpp – which will hold the same values. When I tried to declare these variables in funcs.h (which is included also in main.cpp) I got this error:

1>main.obj : error LNK2005: "int myVariable" (?myVariable@@3HA) already defined in funcs.obj
1>Path to my program : fatal error LNK1169: one or more multiply defined symbols found

which is quite obvious, because it would create variable in funcs.cpp and main.cpp. When I tried to declare variables this way:

//funcs.cpp
int myVariable;

//main.cpp
int myVariable;

And both of them are global I’m getting exactly the same error as above. What interested me is that both variables are not visible in opposite file, so why it is wrong to have variables with the same name? As far as I know there is no possibility to reference to myVariable in funcs.cpp from main.cpp and reverse. And my second question is – what is the best way to solve my problem, because what I did is just renamed some of these variables (there are many of them) and add functions in funcs.cpp like setNewValue(int newValue) which I can invoke from main.cpp but to be honest – I’m not proud of that.

I use Microsoft Visual Studio 2012, C++/OpenGL.

  • 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-13T01:57:22+00:00Added an answer on June 13, 2026 at 1:57 am

    Using C++:

    • If you’re trying to share the global variable between the two files, then declare it in one of them, e.g. main.c, just as you’re doing now, and in the other file declare the variable as extern. This way, the variable has been declared in one file, and the same variable can be used in other files, and you won’t have problems with multiple definitions of that variable.

    • If you want to use a global variable with the same name in each file, such that the files don’t share that variable, then use an anonymous namespace to limit the visibility of the variables:

      namespace
      {
          int myVariable;
      
          // ...
      

    Standard C: For the sake of completeness, if you’re using standard C, instead of using namespaces to limit the visibility of a variable, you can use the static keyword: as above, if you want to use a global variable with the same name in each file, and you don’t want the files to share that variable, then you’ll need to declare them as static. This limits their visibility to the current file.

    Sharing the global using extern works similarly in C.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.