Hi,

How do I use the generator in the file? I keep randomc.h as a header file in the Visual c++ project. My project has several header files and several .cpp files, many of which require to use the random no. generator. I have copied all the files in the zip folder into the folder for my project files.
Do I write this in each file that uses the generator?

#ifndef MULTIFILE_PROJECT
// If compiled as a single file then include these cpp files, 
// If compiled as a project then compile and link in these cpp files.
   // Include code for the chosen random number generator:
   #include "mersenne.cpp"
   // define system specific user interface:
   #include "userintf.cpp"
#endif


#include <time.h>                      // define time()
#include "randomc.h"

The compling is ok. But I get a lot of linking errors, e.g.,

GABaseSolver.obj : error LNK2005: "private: void __thiscall CRandomMersenne::Init0(int)" (?Init0@CRandomMersenne@@AAEXH@Z) already defined in GA1DArraySolution.obj

Please help.

Dani AI

Generated

The LNK2005 messages mean the same symbol is being defined into more than one object file. Header guards or #pragma once (good points from and ) prevent multiple processing of a header inside one translation unit, but they do not stop an implementation file from being pasted into every translation unit when a .cpp is #included. In this thread the implementation files (the Mersenne code) are getting compiled into each .obj, so the linker finds duplicate definitions.

A safe multi-file fix is to keep only declarations in headers and compile each implementation .cpp exactly once. One pattern:

// randomc.h  (declaration only)
class CRandomMersenne;          // forward declaration if needed
extern CRandomMersenne globalRng;
// mersenne.cpp  (single definition)
#include "randomc.h"
CRandomMersenne globalRng;      // one & only definition

An alternative for a header-only convenience is an inline accessor (C++11+), which provides a single instance without a separate .cpp:

// header-only accessor (inline avoids ODR problems; static init is thread-safe since C++11)
inline CRandomMersenne& globalRng() {
    static CRandomMersenne rng;
    return rng;
}

Practical steps for Visual C++ projects: remove any #include of implementation .cpp files from headers, add the implementation .cpp files to the project (Project → Add Existing Item) so they get compiled once, then rebuild. If the distributed code expects a single-file build, enable that mode via the library’s build macro only when intentionally compiling everything into one TU. Finally, consider modern alternatives (std::random) or per-thread RNGs (thread_local) if concurrency is required — and verify the RNG class’s thread-safety before sharing a single global instance across threads.

Recommended Answers

All 2 Replies

in every header file it is a good idea to use inclusion guards. so to stop it from getting defined all the time just put

#ifndef RANDOMC_H
#define RANDOMC_H
//  all the code in randomc.h goes here
#endif

this will make sure that the file is only included the first time and every other time #include "randomc.h" is encountered the compiler will skip that file because it is already defined. I've found this makes things easier down the road and saves some debugging time.

Some compilers that are stupid the gaurds still wont work, if that is so try using pragma once at the top.

#pragma once
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.