I recently wanted to build some PostgreSQL C stored functions on a Win32 machine using the Microsoft Windows SDK. I wanted to build with the Microsoft compiler (Visual C) using nmake files, but without involving the Visual Studio IDE.
To do this you need to take your C functions and compile them into a DLL. When you build a DLL with the Microsoft toolchain the functions are not automatically exported from the DLL.
You need to either:
- Mark the functions as being exported on the linker command line. This involves listing each exported function on the command line of cl.exe Since I did not try this I’m not going to describe the process
- Mark the functions as being exported in the C file by changing the function declaration. Searching the internet I found a few examples of how to do this with the PGDLLEXPORT macro but PostgreSQL kept crashing when it called into my functions built this way.
- Create a module definition file as I describe below
Say you have a function
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(_myFunc);
Datum _myFunc(PG_FUNCTION_ARGS);
You then create a module definition file, mymodule.def
EXPORTS
_myFunc
Each function that your DLL exports (that would be each function that will be named in a ‘CREATE FUNCTION’ statement) needs to be listed in the .def file under the EXPORTS section.
When linking you would use a command similar to
cl.exe /DEF:mymodule.def /DLL c:\Postgresql\9.0\lib\postgres.lib mymodule.obj