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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:07:34+00:00 2026-06-13T02:07:34+00:00

I have a program with two external files in addition to main.cpp and a

  • 0

I have a program with two external files in addition to main.cpp and a header of constants. So in total four files. They contain the following code:

main.cpp

#include <iostream>
using namespace std;

int ext1_func();
int ext2_func();

int main()
{
    int i;
    i = ext1_func();
    cout <<  i << endl;

    i = ext2_func();
    cout << i << endl;

    return 0;
}

ext1.cpp

#include "const.h"
int asd1=1;

int ext1_func()
{
   return temp_int;

}

ext2.cpp

#include "const.h"
int asd2 = 2;

int ext2_func()
{
   return temp_int;
}

const.h

#ifndef CONST_H
#define CONST_H

const int temp_int=1;

#endif

My desired is the following:

1) Any variables declared in ext1.cpp should be known only to functions within ext1.cpp and likewise for ext2.cpp. So “asd1” must only be known to “ext1_func” and “asd2” for “ext2_func”.

2) The functions in “exp1.cpp” and “ext2.cpp” must be able to see all values defines in “const.h”

I believe that the code I have written and attached satisfies these requirements, but I would like to ask if I am doing it correctly? Is there an easier way to obtain the desired behavior than what I have suggested?

Thanks in advance.

Niles.

  • 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-13T02:07:35+00:00Added an answer on June 13, 2026 at 2:07 am

    Within compilation unit level, the symbols you declare at “global” scope are all linked in to the global variable space.

    The old way to avoid this was to declare them static. The new way is to put them into an anonymous namespace. They can also be declared static there too.

    ext1.cpp

    namespace {
      /*static*/ int asd1=1;  
    }
    

    The const in the header is ok, constants declared this way have a special status, in fact that compiler can substitute in the code the value. Ideally though, you should at least wrap them in a (named) namespace to avoid polluting the global one. Modifying the value (using const_cast) would be undefined behaviour.

    If it were non-const and you really wanted a global (not generally a good idea) you would need to use the extern keyword in the header, then create its instance in a single compilation unit.

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

Sidebar

Related Questions

I have a program that compares two files. I ran visual studio analysis and
Say your program is composed of two source files (main.c and auxiliary.c) and two
In my program I have two classes: Collector and Entity. Collector stores Entities in
I have a basic program that compares two strings : #include <string> #include <iostream>
I have a program that generates/'rolls' two dice. I would like to output these
I have a program where I am generating two double numbers by adding several
I have a program that compares variables from two structs and sets a bit
I have a program where I want to draw a line between two points.
Hi I have a program written in C++ in which one or two functions
I have my program that can draw rectangles. I have two problems I can'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.