3
votes

I have searched some articles about C++ (COM-like) ABI Interface of DLL, for example: "Exporting C++ classes from a DLL" and "Binary-compatible C++ Interfaces", they're very nice and helpful, but they didn't talk about namespace.

Does namespace break ABI ?

namespace Foo {

    class Listener {
        virtual void e1() = 0;
        virtual void e2() = 0;
    };

    class Interface {
        virtual void setListener(Listener*) = 0;
        virtual int f1() = 0;
        virtual int f2() = 0;
    };

}

extern "C" Foo::Interface* SOME_API createFooInterface();

I have no idea, does it work ?


Sorry, I'm just not being clear, my English writing ability is not very well.

In my code snippet, extern "C" is preventing name mangling in C++ DLL exports, name mangling is incompatible between C++ compilers, the two articles mentioned above are talking about this question.

My meaning about "break ABI" is: I can do this in MSVC, I can use namespace for my C++ interface classes, I can export a DLL from this code, but I am not sure this interface is still ABI - A Binary-compatible interface between compilers, it's not about two namespaces, it's about two or more compilers.

1
Why don't you give it a try, and tell us?DevSolar
@DevSolar Because of complexity. I can't try each complier and a try will not be enough. I don't know is there some de facto standard about this.amanjiang
Well, if your example actually allowed that test, I could tell you the results of half a dozen compilers, which should be a good start even if it isn't chapter and verse of the standard. Since it doesn't, take this as a subtle hint that your question leaves something to be desired, i.e. research effort. Or, to put it differently: How, in your mind, should Foo::Interface * be flattened to "C" linkage, i.e. without name mangling, and still be distinguishable from e.g. Bar::Interface *? How could the C function createFooInterface() return a C++ construct (a namespaced type)?DevSolar
@amanjiang: Tests are effective in proving that something does not work, they just can't help you prove that something will always work. In this case, DevSolar has a point: A C++ namespace change will affect the C++ ABI. I don't see why you are writing extern "C" when discussing C++ ABI's, though. That defeats the point.MSalters
Namespaces are a compiler concept and disappear after compilation. Talking about ABI is just not appropriate, C++ rules apply here. Virtual dispatch tables can be emulated in C, there's just nothing much fun about it. Tooling helps, like MIDL in the COM world which can auto-generate the C declarations from the IDL.Hans Passant

1 Answers

2
votes

It has to. Since two functions can be identical apart from their namespace, the ABI must be able to distinguish the two. I.e. they must have unique names. Thus, changing the namespace deletes one function and introduces another, with the compiler having no idea of the relation between the two. Compilers don't read your Git or SVN comments.