Pregunta

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...

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top