C++ export mangled names

106 Views Asked by At

I have written some template functions, and I would like to export every single (explicitly instantiated) version of them.

Python_wrapper.cpp will be a DLL.

Templates.h

namespace PEParserNamespace {
    template<typename T, class PEParserBaseImpl>
    inline PEParserBaseImpl& openFile(T lpFileName, PEParserBaseImpl* pPEParserBaseImpl) noexcept {
        return *pPEParserBaseImpl;
    }
}

Python_wrapper.cpp

//explicit instantiation
namespace PEParserNamespace {
    template PEParser & _cdecl openFile<const wchar_t*, PEParser>(const wchar_t*, PEParser*) noexcept;
    template PEParser & _cdecl openFile<const char*, PEParser>(const char*, PEParser*) noexcept;
}

The problem is, when using __declspec(dllexport) I will get mangled names.

There are several methods for exporting a definition:

  1. __declspec(dllexport) to use in source code
  2. An EXPORTS statement in a .def file
  3. A comment directive in the source code, #pragma comment(linker, "/export: definition ").

extern "C" would result in a linker error because of the overloaded function (openFile).

The second approach (using an EXPORTS statement in a .def file) would force me to manually find out the mangled name of each function, and the mangled names can change between compiler versions (as far as I know). So this is not very satisfying.

The best approach I have found so far is to use #pragma comment(linker, "/export: definition ") as follows:

Templates.h

namespace PEParserNamespace {
    template<typename T, class PEParserBaseImpl>
    inline PEParserBaseImpl& openFile(T lpFileName, PEParserBaseImpl* pPEParserBaseImpl) noexcept {
#pragma comment(linker, "/EXPORT:" __FUNCTION__"someString" "=" __FUNCDNAME__  )
        return *pPEParserBaseImpl;
    }
}

The only problem is that this approach generates one, and only one, export but I need an export for every single overload (generated by the compiler).

My last idea is to do some linker script magic (just an idea).

0

There are 0 best solutions below