0

In C++, is it possible to write code / a Makefile that exports a function from a pre-written C++ file to a shared object - i.e. a DLL / SO file.

As an example, let's assume I have the follow file and function definitions.

# funcs.cpp
int add(int x, int y)
{
    return x + y;
}

To make a Windows DLL, I can modify the above by changing the first line to:

extern "C" __declspec(dllexport) int add(int x, int y)

and using a Makefile containing:

#Makefile
dll: funcs.cpp
    g++ -shared funcs.cpp -o output.dll

However, is this possible for the situation where I cannot modify the funcs.cpp file. i.e. How can I package pre-written functions into a shared object please?

NOTE: This question got closed as being already answered here: Exporting functions from a DLL with dllexport However, that question is different and it's not clear how to export to DLL without adding _declspec(dllexport) to the original cpp file. If it's easy and I'm in fact missing something, perhaps someone could post the answer or otherwise, can the question be re-opened please?

  • 2
    What about a build process that takes the original `.cpp` code, *augments it* as necessary, and then compiles that? – tadman Jan 29 '21 at 22:25
  • Thanks. That would be ideal but could you suggest how to achieve that please? I'm guessing a makefile? Or would this be .cpp file that includes that original .cpp somehow? – Qconfused9102 Jan 29 '21 at 22:31
  • Could be a Python script, or something you write in C++ to do the parsing and inclusion. You'll need a way of detecting function signatures, but there are surely tons of tools capable of doing that. It really depends on how bizarre your code base is. There's plugins for Visual Studio Code that detect C++ function definitions, so it can be done. – tadman Jan 29 '21 at 22:33
  • Ok, thanks. But that sounds a bit messy to have Python / scripts to do this. I was hoping there would be a nice clean and quick way with C++ or makefiles. e.g. create a new `.cpp` file, include the function and then define a new function name or something that I can export to the dll. I'd prefer to avoid Visual Studio if possible, favoring g++ to keep it platform independent. Do you have any suggestions for the tools you mention that might do this please? – Qconfused9102 Jan 29 '21 at 22:38
  • I'm just saying Visual Studio Code has examples of how it's done, as the plugins are largely open-source, not to make it a dependency. If it can figure out where functions are defined to make an index, so can you. What you're asking for is inherently a bit messy. You can't compile something, then redefine how those functions are declared post-facto. – tadman Jan 29 '21 at 22:54

1 Answers1

0

There are several ways to export a function, explained here: https://learn.microsoft.com/en-us/cpp/build/reference/export-exports-a-function?view=msvc-160

The two you can use without modifying the source code are:

  • An EXPORTS statement in a .def file
  • An /EXPORT specification in a LINK command

Microsoft says the first is preferred over the second, and I would agree. Simply create a .def file to pass to the linker. It will have content like this:

LIBRARY foo
EXPORTS
    add

Pass the name of the .def file to the linker using the /DEF option.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436