I made a C++ class that I’ve put in its own static library.
I also decided to create a minimal header file that would allow other people to see the public: part of the class. So I basically took the original header file (which is rather long, contains the private: and public: parts of the class, etc.), and stripped out everything but the public: part (which is short, only the constructor/destructor and one public function).
To test things out I’ve created a dummy project that would use the library. The thing is, whenever I use the minimal header file in that project, it crashes with messages like:
test(44349) malloc: *** error for object 0x7fdab2c242e8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
But whenever I include the original header file, it works fine.
What could be the problem? It compiles fine (no warnings even with -Wall) using both the minimal and the original header files.
That doesn’t work and violates the one-definition rule: Every class must have precisely one definition, and each translation unit must see the exact same definition, or otherwise your program is ill-formed, and worse, no diagnostic is required.
You might be better served with the PIMPL idiom, by which you split your class into two parts, and you do not need to expose the implementation component:
See Herb Sutter’s GotW #101 for a nice general-purpose framework for PIMPL classes.