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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:49:48+00:00 2026-06-12T16:49:48+00:00

Sorry if this is a duplicate, but the ones I found… well, I thought

  • 0

Sorry if this is a duplicate, but the ones I found… well, I thought I tried the same solution, but to no avail. Anyhow, I recently tried moving my implementation of my commonly-used random functions, to a single header file: I know how the functions work, doubt I will change them any time soon, and so I figured, less files the better (and the functions are pretty short).

So, I tried it. Stuck the contents of the .cpp file into a new file, and added guards. Then just include it and voila, it works. Unfortunately, I included it twice, and discovered, I was distributing copies of the same function to every file, thus the error. (still get slightly confused why that is with the header guards since you end up with one copy anyway, but I guess it does). Anyway, I googled around (and SO’ed), and discovered if you declare them as inline – which makes sense in this situation – then the preprocessor (or is that also the linker?) would do it right. Now that I think of it… as a small question, are DEFINES local to the file, or global in scope? (i.e., if I include a header in two files with a header guard that defines… do both files include it since the defines occurred in different files, or only once? If it did get included twice, you would get multiple declarations of (but not defines) the same function/class/structure, which I thought throws an error but I guess it does not in different files? Otherwise, though, it might not be included and it does not know what it is. So, to re-ask, are DEFINES local to a file? )

Here is what I have in the file whose functions get duplicated (thought only containing one function)

#ifndef RANDOM_H
#define RANDOM_H

//needed for getting system time and for random generation functions rand and srand
#include <time.h>
#include <stdlib.h>

//this function returns a random number between an inclusive range
inline int randomRange(const int & min, const int & max)
{
    if (max >= min)
        return ((rand() % (max+1-min))+min);
    else
        return ((rand() %  (min+1-max))+max);
}

#endif

Then I just have #includes in two files, on this file (which is “random.h”)

What am I missing?

EDIT: Error message was:

multiple definition of `randomRange(int const&, int const&)’|

Whilst taking a small break, I had closed the IDE. Upon reopening, everything was fixed. Apparently, adding inline DID fix it, but, unlike the cpp files when run, header files (if not added to the project) are not updated unless you manually save them. After saving, it worked. Sorry, everybody.

Lesson of the day: When fixing such errors, make sure your files are updated…

  • 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-12T16:49:50+00:00Added an answer on June 12, 2026 at 4:49 pm

    Macro definitions are local to a translation unit: When compiling a source file the preprocessor expands the various preprocessor directives (#define, #include, etc.) and passes the result to the actual compiler. This is done once for each file not just once. The reason to have include guards is that it is quite likely that the same header gets included twice in the same translation unit. For example, if you have type, say, Point, which is used by classes Rectangle and Triangle and you include both the headers for Rectangle and Triangle, you end up with a translation unit including the header for Point twice.

    When you define a function in a header, you need to indicate to the compiler that it shouldn’t make it a globally visible definition. The normal way is to use inline for this which also indicates to the compiler that it should try to not to call the function if that makes sense. An alternative to the use of inline is to make the function static but in that case you’d actually really end up with multiple copies of the function in your final executable: even though the compiler may use an inline version of the function in every translation unit, at link time one is selected and used from everywhere (and hopefully all other copies are discarded). The difference can be seen, e.g., when you have a local static variable in your function:

    inline int f() {
        static int rc(0);
        return ++rc;
    }
    static int g() {
        static int rc(0);
        return ++rc;
    }
    

    When you call these function f() and g() from different translation units the latter will have a counter per file while the former has a global counter.

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

Sidebar

Related Questions

Sorry if this is a duplicate question...I've searched around and found similar advice but
I know this is duplicate but sorry as solution given on other post seems
I'm sorry if this is a duplicate question, I have found some similar ones,
I am sorry if this is a duplicate but I was not able to
Sorry if this is duplicate,I would think it would be but couldn't find anything.
I'm sorry if this is a duplicate, but the question search terms are pretty
I am sorry if this is a duplicate or too elementary, but how do
I am sorry if this is a duplicate, but I cannot find the answer
Sorry, I know this is probably a duplicate but having searched for 'python regular
First off, I am sorry if this is a duplicate, but every time I

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.