[SOLVED] Need help fixing a 'One Definition Rule' violation


So I wrote and published a library some time ago called BlockNot. It started out as something fairly simple and so I published it as a small single header file - no CPP file.

Then, as time went on, some changes were made to it for improving its capabilities and the last somewhat major change was to add a method to the library that, when called, affects all instantiations of the library. Specifically, one method can change a specific variable in all instantiates of the library.

Now, someone has pointed out that when they use the header file in more than one code file in a single project, it won't compile, and upon further inspection, he pointed out that this last modification to the library, POSSIBLY violates the One Definition Rule (he wasn't absolutely sure but that's what he suspected) ... I'm not an advanced C programmer by any stretch of the imagination and had never heard of this rule before, so I educated myself and from what I have learned, it seems to me that if I were to break the library up into a header file and a code file, that I could pretty much make sure that the rule is followed since that method (having a .h and a .cpp file) is somewhat there for that purpose.

I think I understand the concepts of what the header file is for and what the .cpp file is for ... where the header file declares objects and the code file defines what's in the objects - and the compiler manages the links between the two, and that the implementation of this method was created to make things more efficient for the compiler ... in a nutshell...

But after spending some hours trying to split the library up, I seem to be stumped with how to handle this method that is the one method to rule them all kinda thing ...

If I remove that addition to the library along with all of the related variables and methods that support it, then the library works fine when split into two files ... it's a very basic library as libraries go...

What I am hoping to find here is someone who understands the nature of the One Definition Rule who would be willing to take a look at the library and tell me where it's gone wrong and hopefully how to correct it.

Here is the link to the library

The method in question as at the bottom and it's called resetAllTimers

Thank you,

Mike Sims

So your challenge are the class variables

BlockNot* BlockNot::firstTimer   = nullptr;
BlockNot* BlockNot::currentTimer = nullptr;

Which get defined multiple times if you inject the cpp file into a source code file.

You should indeed just build a .h with the class declaration (prototype of the methods and member variables) and then have the cpp in which you define the class variables.

OK, I'll give that a shot and see what happens.

I got it compiling just fine, thank you for that ... now I will test and if it works, I'll push it out.


I got everything tidy'd up and working correctly. Thank you for looking at the code and pointing out the error. It's been properly fixed and published!

Well done !

1 Like