Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded functions in C++ DLL def file

Tags:

c++

c

dll

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

LIBRARY "MyLib"
EXPORTS
  Foo
  Bar

with the code defined as this, for example:

int Foo(int a);
void Bar(int foo);

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

int Foo(int a, int b);

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() ?

Edit: To be clear, this is on Windows using Visual Studio 2005

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.

like image 752
Adam Haile Avatar asked Aug 25 '08 14:08

Adam Haile


People also ask

What are overloaded functions in C?

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.

What is DEF file in DLL?

A module-definition or DEF file (*. def) is a text file containing one or more module statements that describe various attributes of a DLL. If you are not using the __declspec(dllexport) keyword to export the DLL's functions, the DLL requires a DEF file.

Does C support overloaded functions?

No, C doesn't support any form of overloading (unless you count the fact that the built-in operators are overloaded already, to be a form of overloading). printf works using a feature called varargs.

What is overloaded function give example?

Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is ...


1 Answers

Function overloading is a C++ feature that relies on name mangling (the cryptic function names in the linker error messages).

By writing the mangled names into the def file, I can get my test project to link and run:

LIBRARY "TestDLL"
EXPORTS
    ?Foo@@YAXH@Z
    ?Foo@@YAXHH@Z

seems to work for

void Foo( int x );
void Foo( int x, int y );

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) ?

The mangled names are non-portable, I tested with VC++ 2008.

like image 166
Timbo Avatar answered Sep 22 '22 19:09

Timbo