<p>I'm writing a C/C++ DLL and want to export certain functions which I've done before using a .def file like this</p>

<pre><code>LIBRARY "MyLib"
EXPORTS
  Foo
  Bar
</code></pre>

<p>with the code defined as this, for example:</p>

<pre><code>int Foo(int a);
void Bar(int foo);
</code></pre>

<p>However, what if I want to declare an overloaded method of Foo() like:</p>

<pre><code>int Foo(int a, int b);
</code></pre>

<p>As the def file only has the function name and not the full prototype I can't see how it would handle the overloaded functions.  Do you just use the one entry and then specify which overloaded version you want when passing in the properly prototyped function pointer to LoadLibrary() ?</p>

<p>Edit:  To be clear, this is on Windows using Visual Studio 2005</p>

<p>Edit: Marked the non-def (__declspec) method as the answer...I know this doesn't actually solve the problem using def files as I wanted, but it seems that there is likely no (official) solution using def files.  Will leave the question open, however, in case someone knows something we don't have overloaded functions and def files.</p>

[{'answer_id': 26121, 'author': 'Graeme Perrow', 'author_id': 1821, 'author_profile': 'https://Stackoverflow.com/users/1821', 'pm_score': 4, 'selected': True, 'text': "<p>In the code itself, mark the functions you want to export using __declspec(dllexport). For example:</p>\n\n<pre><code>#define DllExport __declspec(dllexport)\n\nint DllExport  Foo( int a ) {\n  // implementation\n}\nint DllExport Foo( int a, int b ) {\n  // implementation\n}\n</code></pre>\n\n<p>If you do this, you do not need to list the functions in the .def file.</p>\n\n<p>Alternatively, you may be able to use a default parameter value, like:</p>\n\n<pre><code>int Foo( int a, int b = -1 )\n</code></pre>\n\n<p>This assumes that there exists a value for b that you can use to indicate that it is unused. If -1 is a legal value for b, or if there isn't or shouldn't be a default, this won't work.</p>\n\n<p>Edit (Adam Haile):  Corrected to use __declspec as __dllspec was not correct so I could mark this as the official answer...it was close enough.  </p>\n\n<p>Edit (Graeme): Oops - thanks for correcting my typo!</p>\n"}, {'answer_id': 26142, 'author': 'Timbo', 'author_id': 1810, 'author_profile': 'https://Stackoverflow.com/users/1810', 'pm_score': 4, 'selected': False, 'text': '<p>Function overloading is a C++ feature that relies on name mangling (the cryptic function names in the linker error messages).</p>\n\n<p>By writing the mangled names into the def file, I can get my test project to link and run:</p>\n\n<pre><code>LIBRARY "TestDLL"\nEXPORTS\n    ?Foo@@YAXH@Z\n    ?Foo@@YAXHH@Z\n</code></pre>\n\n<p>seems to work for</p>\n\n<pre><code>void Foo( int x );\nvoid Foo( int x, int y );\n</code></pre>\n\n<p>So copy the C++ function names from the error message and write them into your def file. However, the real question is: Why do you want to use a def file and not go with __declspec(dllexport) ?</p>\n\n<p>The mangled names are non-portable, I tested with VC++ 2008.</p>\n'}, {'answer_id': 26299, 'author': 'Mat Noguchi', 'author_id': 1799, 'author_profile': 'https://Stackoverflow.com/users/1799', 'pm_score': 2, 'selected': False, 'text': "<p>There isn't a language or version agnostic way of exporting an overloaded function since the mangling convention can change with each release of the compiler.</p>\n\n<p>This is one reason why most WinXX functions have funny names like *Ex or *2.</p>\n"}, {'answer_id': 29909, 'author': 'Christopher', 'author_id': 3186, 'author_profile': 'https://Stackoverflow.com/users/3186', 'pm_score': 2, 'selected': False, 'text': "<p>There is no official way of doing what you want, because the dll interface is a C api.</p>\n\n<p>The compiler itself uses mangled names as a workaround, so you should use name mangling when you don't want to change too much in your code.</p>\n"}, {'answer_id': 342055, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I had a similar issue so I wanted to post on this as well.</p>\n\n<ol>\n<li><p>Usually using </p>\n\n<pre><code>extern "C" __declspec(dllexport) void Foo();\n</code></pre>\n\n<p>to export a function name is fine. \nIt will <em>usually</em> export the name\nunmangled without the need for a\n.def file.  There are, however, some\nexceptions like __stdcall functions\nand overloaded function names.</p></li>\n<li><p>If you declare a function to use the\n__stdcall convention (as is done for many API functions) then</p>\n\n<pre><code>extern "C" __declspec(dllexport) void __stdcall Foo();\n</code></pre>\n\n<p>will export a mangled name like\n_Foo@4.  In this case you may need to explicitly map the exported name\nto an internal mangled name.</p></li>\n</ol>\n\n<p>A. How to export an unmangled name.  In a .def file add</p>\n\n<pre><code>----\nEXPORTS\n    ; Explicit exports can go here\n\n    Foo\n-----\n</code></pre>\n\n<p>This will try to find a "best match" for an internal function Foo and export it.  In the case above where there is only\none foo this will create the mapping </p>\n\n<p>Foo = _Foo@4</p>\n\n<p>as can be see via dumpbin /EXPORTS</p>\n\n<p>If you have overloaded a function name then you may need to explicitly say which function you want in the .def file\nby specifying a mangled name using the entryname[=internalname] syntax. e.g.</p>\n\n<pre><code>----\nEXPORTS\n    ; Explicit exports can go here\n\n    Foo=_Foo@4\n-----\n</code></pre>\n\n<p>B. An alternative to .def files is that you can export names "in place" using a #pragma.</p>\n\n<pre><code>#pragma comment(linker, "/export:Foo=_Foo@4")\n</code></pre>\n\n<p>C. A third alternative is to declare just one version of Foo as extern "C" to be exported unmangled.  See <a href="http://blogs.msdn.com/shamit/archive/2005/02/11/371421.aspx" rel="noreferrer">here</a> for details.</p>\n'}, {'answer_id': 15540686, 'author': 'null', 'author_id': 1999454, 'author_profile': 'https://Stackoverflow.com/users/1999454', 'pm_score': 2, 'selected': False, 'text': '<p>Systax for EXPORTS definition is:</p>\n\n<pre><code>entryname[=internalname] [@ordinal [NONAME]] [PRIVATE] [DATA]\n</code></pre>\n\n<p><strong>entryname</strong> is the function or variable name that you want to export. This is required. If the name you export is different from the name in the DLL, specify the export\'s name in the DLL with internalname.</p>\n\n<p>For example, if your DLL exports a function, func1() and you want it to be used as func2(), you would specify:</p>\n\n<pre><code>EXPORTS\nfunc2=func1\n</code></pre>\n\n<p>Just see the mangled names (using Dependency walker) and specify your own functions name.</p>\n\n<p>Source: <a href="http://msdn.microsoft.com/en-us/library/hyx1zcd3(v=vs.71).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/hyx1zcd3(v=vs.71).aspx</a></p>\n\n<p>Edit: This works for dynamic DLLs, where we need to use GetProcAddress() to explicitly fetch a functions in Dll.</p>\n'}]
['https://Stackoverflow.com/questions/26098', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/194/']