简体   繁体   English

使用Microsoft Visual Studio在Windows 64位上为PostgreSQL数据库创建C函数dll

[英]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. 我正在尝试根据此处给出的说明创建我的第一个dll: https : //en.scribd.com/doc/40725510/Build-PostgreSQL-C-Functions-on-Windows该函数很简单,并且可以在其他电脑。

#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. 只要我在配置管理器中将Win32用作Active Solution Platform,构建就会成功。 However, when I try to create function in pgAdmin by following code: 但是,当我尝试通过以下代码在pgAdmin中创建函数时:

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". 我收到错误“%1不是有效的Win32应用程序”。 That is expected, because I use 64 bit version of PostgreSQL. 这是预料之中的,因为我使用的是64位版本的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. 但是,如果我将解决方案平台切换到x64(如果我没有记错的话,这是纠正错误所必需的),则尝试构建progect会导致文件MSVCRTD.lib中出现32个“未解析的外部符号”错误。 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). 我在这里找到了关于类似问题的讨论: https : //social.msdn.microsoft.com/Forums/vstudio/en-US/c1553a42-7d1e-4c74-9156-95768a72e4df/unresolved-externals-after-upgradeing-from- vs2008-to-vs2010?forum = vcgeneral错误似乎是由不同版本的Visual Studio中的库冲突引起的,但是重新安装没有帮助,我也不知道如何手动更正路径(而且我仍然不完全是确保我的问题是相同的)。 The paths I use are the same as in the instruction, but they use 64 bit version of PostgreSQL instead: 我使用的路径与指令中的路径相同,但是它们使用的是64位版本的PostgreSQL:

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? 关于如何成功构建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. 嗯,我的同学检查了我的设置,尽管有说明指出它们是必要的,但由于某些奇怪的原因,似乎包含路径中的Microsoft SDK文件夹仍会导致这些错误。 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. 该项目成功构建,没有到Microsoft SDKs文件夹的路径,并且使用生成的dll成功在数据库中创建了功能。 If somebody knew the reason of this conflict in libraries, I would still like to know. 如果有人知道图书馆中发生这种冲突的原因,我仍然想知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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