If you are using Windows, __declspec(dllexport) is necessary to add the export directive to the object file and make the function accessible without using a .def file. If you are using Linux it can be omitted.

The extern "C" construct prevents the compiler to add decorations on the functions' names in the DLL/SO and it is necessary while using C++.

Note: in order to make the code more readable and avoid repetitions, it is a good idea to use a #define as shown in the example

The header of the function can be added to test.h (but it's not necessary):

//test.h
int sum(int, int);

Compile the program to obtain a DLL/SO.

If you are using Windows and Visual Studio, create a new Dinamic-Link Library project and include the two files.

Another way is to use the cl program located in the Visual Studio directory (e.g. C:\Programs\Microsoft Visual Studio 8\VC) with the /LD option: