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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:54:04+00:00 2026-06-18T09:54:04+00:00

I have a multi-file program consisting of main.cpp ext1.cpp ext2.cpp ext3.cpp vars.h As the

  • 0

I have a multi-file program consisting of

main.cpp
ext1.cpp
ext2.cpp
ext3.cpp
vars.h

As the name suggests, main.cpp is the main file, extX.cpp contains various functions and vars.h some global constants.

However, in main.cpp there are also (a few!) variable declarations, but they must only be within the scope of main.cpp — that is why I haven’t placed them in vars.h

I want to reduce the amount of code in main.cpp (for clarity-issues). I am looking for a way to declare these variables inside a header of some sort, but in a way that it is only visible to main.cpp.

Is it correctly understood that if I place them all inside e.g. vars_main.h (with no external keyword) and just include “vars_main.h”, then I have achieved my goal?

Is it considered to be “correct” C++-style to do it this way?

  • 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-18T09:54:05+00:00Added an answer on June 18, 2026 at 9:54 am

    If those variables are used only in main(), then yes, you could do that. But I would not go as far as considering it a “correct C++ style”.

    If one day you will end up including that header file into another translation unit (maybe because you will need to share just one of those variables), the linker will start complaining about multiple definitions.

    At that point, to overcome this, you could use the static keyword to give those variables internal linkage and workaround the problem of multiple definitions. This way, however, each translation unit (.cpp file) will hold its own copy of those variables, which is probably not what you want, especially if they are not constant – just for the record, global constants have internal linkage by default, so you won’t need to explicitly qualify them as static.

    The normal practice here is either to leave those variable definitions in main(), or to have one header which contains only extern declarations of those variables, and one translation unit that contains their definitions. Then, all the files which need to access those variable would just import the header with the declarations.

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

Sidebar

Related Questions

I have write a multi-level Preferences in program with xml File. and i want
I am to compile a multi-file C++ program, and have the following error; Undefined
I am writing a multi-agent system in 2APL. In my agent file, I have
I have a 3 file program, basically teaching myself c++. I have an issue.
I have implemented a multi-threaded program which involves spawning a thread for each user,and
I currently have a multi-threaded program that crawls websites and writes their text to
I have a multi-threading Python program, and a utility function, writeLog(message) , that writes
I have a multi threaded program in C that was working well but was
I try to type a single program have (client) and (multi-client server) to send
I want to save data to a file in my elisp program. I have

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.