Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a slim version of my header to be included with the library?

Tags:

c++

oop

What I mean is my real header file can look like this:

#include "some_internal_class.h"

class MyLibrary {
    Type private_member;

    void private_function();
public:

    MyLibrary();
    void function_to_be_called_by_library_users();
};

Now I want to produce a dynamic library containing all the necessary definitions. and I want to ship with it a single header instead of shipping every single header I have in my library.

So I was thinking I could create a slim version of my header like so:

class MyLibrary {
public:     
    MyLibrary();
    void function_to_be_called_by_library_users();
};

Headers are just declarations anyway right? they're never passed to the compiler. And I've declared what the user will be using.

Is that possible? If not, why not?

like image 958
Adham Zahran Avatar asked Sep 14 '17 16:09

Adham Zahran


2 Answers

This is a One Definition Rule violation. The moment you deviate by a single token.

[basic.def.odr]/6

There can be more than one definition of a class type, [...] in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

  • each definition of D shall consist of the same sequence of tokens; and

Your program may easily break if you violate the ODR like that. And your build system isn't at all obligated to even warn you about it.

like image 58
StoryTeller - Unslander Monica Avatar answered Sep 28 '22 08:09

StoryTeller - Unslander Monica


You cannot define a class twice. It breaks the One Definition Rule (ODT). MyLibrary does that, unfortunately.

they're never passed to the compiler

They will. Members of a class must be known at compile time, so that the compiler can determine the class's size.

like image 26
gsamaras Avatar answered Sep 28 '22 08:09

gsamaras