1
votes

I must use the C++ OpenVPN3 library to develop a Qt application with OpenVPN capabilities. The problem is that I need to make a class that derives from both QObject and an abstract class provided by this library.

class Client : public QObject, public ClientAPI::OpenVPNClient {
    Q_OBJECT
{
 . . .
}

All of the types I need to use are kept in one big source (cpp) file called ovpncli.cpp. This becomes problematic when I'm now including a source file in a header file, because it causes a multiple definition error when the translation units are generated by the compiler. Classes that inherit from QObject must also have a separate definition and implementation unless you add "include myclass.moc" to the end (but this hasn't worked for me as seen here). The library developers have not said much other than "putting the OpenVPN header files only in my class's implementation file", but this is impossible because the class definition is inheriting from a class defined only in the `ovpnfile.

Is there a way I can include a cpp file in a header file while preventing the multiple definition error at the same time?

1
There is no need to include .cpp anywhere. You should use corresponding ovpncli.hpp file instead. - user7860670
@VTT Please refer to the comment I've left on Ben's answer. I would have thought the same thing, but it doesn't work, and according to one of the maintainers you have to use the .cpp file. - Chase
@drescherjm I am the one who opened that issue, and frankly, nothing that I didn't already know was said by them, which is why I'm here. OpenVPN headers cannot be entirely excluded from my class's header file, otherwise it can't inherit from ClientAPI::OpenVPNClient. - Chase
You may have to switch to composition instead of inheritance if you can't get around this bad design. - drescherjm

1 Answers

3
votes

The definition of that class is in ovpncli.hpp not ovpncli.cpp. (Yes, really) So just use the header system the way it was intended.

#include "ovpncli.hpp"

and link with ovpncli.cpp but do not #include it.