Search code examples
c++dllexport

C++ How to specify namespace in def file


"TestDLL.h"

namespace atest
{
    void HelloWorld();
}

namespace btest
{
    void HelloWorld();
}

I trying to export the dll function by def file. There are two functions with same name, but in different namespace. I write the def file like this:

LIBRARY "TestDLL"
EXPORTS
    HelloWorld @1

The visual studio shows "error LNK2001: unresolved external symbol HelloWorld". I can't find any useful information about how to specify namespace in def file. I want to know how to solve this problem. I would be appreciate for your help.


Solution

  • Def files must contain decorated names. (i.e. function names as the compiler and linker sees them)

    You can read up the details on Microsoft's site, but here's the relevant section:

    If you are exporting functions in a C++ file, you have to either place the decorated names in the .def file or define your exported functions with standard C linkage by using extern "C". If you need to place the decorated names in the .def file, you can obtain them by using the DUMPBIN tool or by using the linker /MAP option

    Personally, I prefer using /MAP. I find it a lot more straightforward than using the swiss army knife that DUMPBIN is.

    For reference here is a link to the documentation of decorated names in general.