0

is there a way to use another name for a function I imported from a dll? Lets say I have a function written in C, that is exported to a DLL:

__declspec(dllexport) int Sum(int x, int y);

and I want to import it into my C++ project under namespace Math, and named Add instead of Sum:

namespace Math {
  __declspec(dllimport) int Add(int x, int y);
}

Now i know this wont link, so is there a possible way to do it?

  • Why? This might be a so-called "XY problem" which mean's you are asking the wrong question. – Ulrich Eckhardt Sep 18 '21 at 21:01
  • Load it manually (don't use the import library) using the `LoadLibrary` `GetProcAddress` combo and you can call it whatever you like. – Richard Critten Sep 18 '21 at 21:18
  • 1
    If you can use a .DEF file, you can change the name of the export, although I think you'll need to use the manged name. See documentation for the [EXPORTS](https://learn.microsoft.com/en-us/cpp/build/reference/exports) section. – 1201ProgramAlarm Sep 18 '21 at 21:41
  • Possible [duplicate](https://stackoverflow.com/questions/22582666/providing-a-function-alias-in-gcc) – 1201ProgramAlarm Sep 18 '21 at 21:45
  • @1201ProgramAlarm I was thinking of a generic way, that applies to language itself, not gcc (im using msvc). something like in C# where you'd have [DLLimport("Library.dll", entryPoint="Sum")] – DeltaJuliet2k Sep 18 '21 at 22:13
  • @Ulrich Eckhardt i am not sure what you mean? i have this c project that generates dll. i'd like to use it in a c++ project, but i'd like a more fitting name / namespace. That is the problem I am facing. – DeltaJuliet2k Sep 18 '21 at 22:15
  • I was thinking you had e.g. collisions, like two libraries defining the same name. In this case though, see the answer below. – Ulrich Eckhardt Sep 19 '21 at 05:39

1 Answers1

1

You can write a simple wrapper:

// header file
namespace Math {
    int Add(int, int);
}

// implementation file
#include "c-library-header.h"
int Math::Add(int x, int y) {
    return Sum(x, y);
}

I've split this into two files in order to avoid including the C header file in exposed code, i.e. to get a cleaner separation of either side of the interface. If you don't care about that, you could as well use just one file of inline wrapper functions.

Concerning the approach you asked about, I'd really consider that an XY problem (search for that term online!). Trying to rename the imported functions is very hackish, at least it is unusual and surprising to the casual reader. Using a set of wrapper function is a common approach though.

Also, for functions that are not as trivial as the example, you sometimes have dynamically allocated returnvalues or error codes that are returned. In both of these cases, the wrapper code would not just provide the same function under a different name. For dynamically allocated returns, it would use either containers or smart pointers, for error codes it would use exceptions. In any case, those are things you can't represent by hacking the linker to rename symbols.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55