简体   繁体   中英

Creating a C function dll for PostgreSQL database on Windows 64 bit using Microsoft Visual Studio

I'm trying to create my first dll according to instructions given here: https://en.scribd.com/doc/40725510/Build-PostgreSQL-C-Functions-on-Windows The function is simple and it is working on other computer.

#include "postgres.h"
#include "fmgr.h"
#include <windows.h>

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

#if defined(_WIN32)
#define DLLEXPORT __declspec(dllexport) 
#else
#define DLLEXPORT
#endif

#define WIN32_LEAN_AND_MEAN
#define NOCOMM

PG_FUNCTION_INFO_V1(fibbonachi);
DLLEXPORT Datum
fibbonachi(PG_FUNCTION_ARGS)
{
    int32 arg = PG_GETARG_INT32(0);
    if (arg > 0 && arg < 100)
    {
        if (arg == 1 || arg == 2)
            PG_RETURN_INT32(1);
        else
        {
            int arr[100];
            arr[0] = 1;
            arr[1] = 1;
            for (int i = 2; i < arg; i++)
                arr[i] = arr[i - 1] + arr[i - 2];
            PG_RETURN_INT32(arr[arg - 1]);
        }
    }
    else
        PG_RETURN_INT32(0);
}

As long as I use Win32 as Active Solution Platform in Configuration Manager, the build succeeds. However, when I try to create function in pgAdmin by following code:

CREATE or REPLACE FUNCTION fibbonachi(integer) RETURNS integer 
AS E'D:\\Homework\\Databases\\PostgreSQL_dll\\Debug\\PostgreSQL_dll.dll', 'fibbonachi'
LANGUAGE C STRICT;

I get an error "%1 is not a valid Win32 application". That is expected, because I use 64 bit version of PostgreSQL. But if I switch the Solution Platform to x64 (which is necessary to correct the error if I'm not mistaken), the attempt to build the progect results in 32 errors of "unresolved external symbols" in file MSVCRTD.lib. I've found discussion about similar problem here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c1553a42-7d1e-4c74-9156-95768a72e4df/unresolved-externals-after-upgrading-from-vs2008-to-vs2010?forum=vcgeneral It seems that errors are caused by conflicting libraries from different versions of Visual Studio, but reinstallation didn't help, and I have no idea how to correct the paths manually (and I am still not entirely sure that my problem is the same). The paths I use are the same as in the instruction, but they use 64 bit version of PostgreSQL instead:

C:\Program Files\Microsoft SDKs\Windows\v7.0\Include
C:\Program Files\PostgreSQL\9.3\include\server\port\win32
C:\Program Files\PostgreSQL\9.3\include\server\port
C:\Program Files\PostgreSQL\9.3\include\server
C:\Program Files\PostgreSQL\9.3\include\internal
+directory of dummy header libintl.h

Any ideas of how to sucsessfully build the x64 progect?

Hm, my classmate checked my settings and it seems that Microsoft SDKs folders from included paths causes these errors for some weird reason, despite the fact that instruction states them being necessary. He doesn't remember where exactly he found this information though. Nevertheless, the problem is solved. The progect builds successfully without paths to Microsoft SDKs folders, and usage of resulting dll successfully creates function in database. If somebody knew the reason of this conflict in libraries, I would still like to know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM