C++: Multiple definition of variable, but not defined somewhere else?

I am currently trying to compile some old code of mine which never made any problems – until now that I work with it again in Ubuntu.(gcc 4.8.2)

The issue is caused by a random number generator in a #define, the file looks basically like this:

**Lights.h:**
#if defined(__linux) || defined(__linux__)
    unsigned int seed = time(NULL);
    #define RND ((double)rand_r(&seed)/RAND_MAX) // reentrant uniform rnd
#endif

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
	#define RND ((double)rand()/RAND_MAX) // uniform rnd
#endif

class Lights {
.... //various structs and functions, using RND
}

**Lights.cpp**
#include Lights.h
....
//implementation of functions, also using RND

This worked always with no problem, but now I get the following error when compiling:

CMakeFiles/luxrender.dir/qtgui/lightSourceCleaner.o: In function `qt_noop()':
/home/.../Lights.cpp:131: multiple definition of `seed'
CMakeFiles/.../mainwindow.o:/home/.../mainwindow.cpp:103: first defined here
CMakeFiles/...mainwindow.o: In function `MainWindow::startLsc()':
mainwindow.cpp:(.text+0x167b4): undefined reference to `Lights::cleanLightGroups(QProgressDialog&, int, int, int, double, double, double, bool, bool, double, double)'
collect2: error: ld returned 1 exit status
make[2]: *** [luxrender] Error 1
make[1]: *** [CMakeFiles/luxrender.dir/all] Error 2

So I’m a bit puzzled here, as there is no redefinition of “seed” in mainwindow.cpp, in fact, line 103 relates to a function I never use in this context (it’s related to logging.)

I have no clue what qt_noop() is (probably smtgh related to the QT GUI, but I never touch this either in this case).

“seed” is not anywhere else defined, only Lights.h has this #define, and Lights.h is only included in mainwindow.cpp.

Any ideas what could be the problem? (As I said, this worked, but it was 2 years ago – did anything eventually change with gcc 4.8.2 ?)


Source: gcc

Leave a Reply