I am writing a DLL library in C ++. It has some function. It takes type & as input, where type is some type (class) defined in the library and not specified for export. If I in a project using this library define this class identically and call a function from dll with an argument — an instance of a class defined in the project, will it work adequately?

Perhaps there are options for how to do it differently?

Or can I make a .hpp file with this class and include it in the project and in the library?

  • not. You expect exciting adventures with debugging code that passes classes across the boundaries of executable modules. Not to mention the distribution / purification of memory in different places. - Igor
  • @Igor And what option then, if I want to make functions that work with project classes in dll? And what will be the problem with the general heder? - Jenssen
  • I, of course, put it too much. With some restrictions - for example, explicitly setting the alignment of members, allocating / clearing the memory to one side of the exe module boundaries, this is possible. But you have to keep this in mind. But the Windows API is an example of a different approach; data is exchanged through simple types and structures. - Igor
  • one
    Possible duplicate question: How to use a custom type in a DLL? - αλεχολυτ

1 answer 1

If I in a project using this library define this class identically and call a function from dll with an argument — an instance of a class defined in the project, will it work adequately?

Yes, this is done. It is only necessary to monitor not only the complete identity of the class definition, but also the identity of such compiler settings as alignment.

Or can I make a .hpp file with this class and include it in the project and in the library?

Of course, it is in this way that the definition of a class is achieved: put the definition of the class in the header file and include it wherever it is needed.

The whole technology is practically no different from the banal use of one class in several translation units. With the only difference that on some platforms it is necessary to make additional efforts to export / import external characters.

  • I did not quite understand about the alignment, I will be grateful for the additional information. And what should I do on MSVS? If I include this header with the class in the dll and in the project, everything will be fine? Simply, this header can be made stats, it will not be possible to use __ declspec (dllexport) from it. Or are there any other mechanisms? - Jenssen