0

I make a C++ graphics engine and I want to distribute it in dll. How can I make dll compatible with other compilers? I read about COM, but this method does not suit me because I do not want to depend on windows headers. The second way that I know - make old C interface, but i'm planning an API in object oriented style, so this method is also not real. Are there any other ways to do this in C++ on windows?

  • 1
    ***How can I make dll compatible with other compilers?*** I believe the simplest way is to compile the `dll`using each compiler you will support. Many packages like `opencv`, `Qt` and `boost` do this. – drescherjm Feb 01 '19 at 16:15
  • Why do you think COM was developed in the first place? And you are targeting Windows, but you don't want to include Windows headers? None of tjis makes sense. –  Feb 01 '19 at 16:19
  • @Neil Butterworth. Im plan target to linux. I also want clients to work with smart pointers and not raw. – push рор Feb 01 '19 at 16:29
  • @drescherjm Does this mean that I should build the engine for all compilers of all versions? – push рор Feb 01 '19 at 16:31
  • 1
    Then edit this: "Are there any other ways to do this in C++ on windows?" And there is no direct way of using the same DLL on Windows and Linux. –  Feb 01 '19 at 16:32
  • 2
    Note that "each compiler" is not just "gcc, clang, msvc", but more like "gcc 6.x.y, ..., gcc 7.z.m, ..., gcc 8.n.o, ..., clang p.q.r, ..., msvc 2013, msvc 2013 x64, ..." etc. – Caleth Feb 01 '19 at 16:36
  • @Neil Butterworth Im ask only about Windows because I plan porting to Linux in far future. As far as I know there is standardized ABI on Linux – push рор Feb 01 '19 at 16:45
  • 1
    "As far as I know there is standardized ABI on Linux" - nope. –  Feb 01 '19 at 16:46
  • 1
    @Neil Butterworth https://itanium-cxx-abi.github.io/cxx-abi/ – push рор Feb 01 '19 at 16:48
  • 1
    It could be also simpler if you can distribute just your source code and use `CMake` for your projects. This will allow users to build your engine using many of the popular compilers and avoid you having to build binaries for multiple versions of msvc, mingw, gcc, clang also 32 / 64 bit. Although these days you may just choose to build 64 bit only. – drescherjm Feb 01 '19 at 18:01

1 Answers1

2

I know why you want to avoid COM. I agree. So the one option left is in fact a pure C interface of your dll. And you really need to take care to stick to pure C linkage (extern "C" declaration).

If you are exporting classes with C++ linkage for your dll, the compiler will need to generate mangled names for the exported symbols. The generation of those mangled names is compiler and version dependent. So in fact this means, you need to deploy your dll for all your supported compiler versions.

Name mangling is not part of the C++ standard and hence incompatible between compiler vendors and sometimes even between compiler versions of the same vendor.

One further thing to remember is, that you will have an even harder time, if you plan to use STL types in your dll's interface. Please see STL Containers and Binary Interface Compatibility for more details.

Back to the C interface

As we were facing the same problem in our team, we decided to go for a design of dll having:

  1. ... a C interface and
  2. ... a separate facade library in accompany

The facade lib can either be a static lib compiled in the dll-consumer's build environment or a header-only library. In either case it is a very thin wrapper around the brittle C interface of the dll and can forward a more object-oriented design. So what is does at least is:

  1. Group related functions to classes.
  2. Translate error codes to exceptions.
  3. Translate STL types (strings, vectors etc.) to native types (const char*, void*, bufferlength, etc.)

This way we could maintain the advantage of dll versions having a compatible interface and no need to take (too) much care of the users' environment.

salchint
  • 371
  • 3
  • 10
  • Are there any guarantees that the C interface will work on all compilers? (link if it's possible). Can I write a header-only object-oriented API? Are C ++ and C interface in different headers? – push рор Feb 01 '19 at 16:58
  • 1
    Ad 1) Sorry, I have no guarantee, but I have Microsoft's description of what these symbol names (decorated names) depend on in the C++ and in the C linkage: https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=vs-2017. Ad 2) Yes, you can write an OO API in header only. Large parts of STL is written that way, although this does not mean, that you need to have templates. Ad 3) Yes, you should have the C interface and the C++ API in separate header files. Just for the sake of clarity for the users of your API. – salchint Feb 01 '19 at 17:58