如何使PostgreSQL加载依赖于DLL的C模块
How to make PostgreSQL to load a C-module that depends on a DLL?
我曾尝试从依赖于Windows中另一个DLL的模块在PostgreSQL中创建C语言函数,但没有成功。
例如,假设我要创建一个 sum
函数,该函数将位于其自己的 DLL 中的number
函数返回的两个数字相加。
number.h
的代码是:
#ifndef NUMBER_H
#define NUMBER_H
#ifdef NUMBER_EXPORTS
#define NUMBER_API __declspec(dllexport)
#else
#define NUMBER_API __declspec(dllimport)
#endif
extern "C" NUMBER_API int number();
#endif
number.cpp
的代码是:
#include "number.h"
extern "C" int number() { return 1; }
sum.cpp
的代码是:
extern "C" {
#include <postgres.h>
#include <fmgr.h>
#include <utils/geo_decls.h>
#include "number.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
__declspec(dllexport) PG_FUNCTION_INFO_V1(sum);
Datum sum(PG_FUNCTION_ARGS)
{
int32 a {number()};
int32 b {number()};
int32 result {a + b};
PG_RETURN_INT32(result);
}
} // extern "C"
假设 PostgreSQL 安装在 C:Program FilesPostgreSQL9.5
上,我使用此build.bat
文件创建number.dll
和sum.dll
:
SET PG_DIR=C:Program FilesPostgreSQL9.5
SET PG_INCLUDES=/I%PG_DIR%include /I%PG_DIR%includeserver /I%PG_DIR%includeserverportwin32
CL number.cpp /EHsc /MD /DNUMBER_EXPORTS /LD
CL sum.cpp /EHsc /MD %PG_INCLUDES% /LD /link number.lib
然后我将两个 DLL 复制到C:Program FilesPostgreSQL9.5lib
并编写用于在 sum.sql
中创建函数的 SQL:
CREATE OR REPLACE FUNCTION sum() RETURNS void AS
'sum'
LANGUAGE C STRICT;
运行psql -U postgres -f sum.sql
后,出现以下错误:
psql:sum.sql:3: ERROR: could not load library "C:/Program Files/PostgreSQL/9.5/lib/sum.dll": The specified module could not be found.
请注意,此错误不同于"找不到文件"错误。PostgreSQL似乎找到了该文件,但没有将其识别为有效的模块。但是,如果我创建一个静态num.cpp
库并将其链接到sum.dll
,则该函数创建成功。换句话说,PostgreSQL只有在它包含所需的所有代码时才加载模块。
为什么会发生这种情况,我怎样才能让PostgreSQL在创建sum
函数时了解number.dll
?
在
启动PostgreSQL服务器之前,依赖DLL的目录必须存在于PATH
中。
转到:
Control Panel
System and Security
System
Advanced System Settings
Advanced
Environment Variables
Path
打开它并添加依赖 DLL 所在的所有目录的完整路径。重新启动 PostgreSQL 服务器并完成,psql
将成功执行 C 模块。
相关文章:
- 如何告诉本机节点模块所需的dll存储在哪里?
- 如何根据 DLL 端的请求卸载 DLL 模块以卸载它?
- 班级大小因模块 (DLL) 而异.如何以及为什么?
- 如何在 lua cpp 模块中调用托管 c++ dll 函数
- 在特定 Windows DLL 模块中分配内存
- C++ ATIOGLXX.pdb 包含查找模块 ATIOGLXX.dll 源代码所需的调试信息
- 哪种方法更好获取静态链接DLL的模块句柄(HMODULE) - 使用GetModuleHandleEx()或LoadLibrary()?
- 清理跨越Windows DLL模块边界的堆分配资源时出现问题
- QtGui4.lib(QtGui4.dll):致命错误LNK1112:模块计算机类型"X86"与目标计算机类型"x64"冲突
- C# 无法加载 DLL(找不到模块结果:0x8007007E)
- 获取传递给子模块(C#.NET dll)中的主模块(VC++ exe)的参数
- DLL 无法加载:0x8007007E找不到模块。不确定导致此依赖项问题的原因
- 如何在 boost 1.57 中安装提升模块(应用程序和 dll)
- .exe vs .dll 作为一个程序的模块
- 模块"MyShellExt.dll"加载失败
- 使用 Boost.Python 从 DLL 动态加载 python 模块
- 查找dll的依赖模块
- 在插件(DLL)中使用Qt网络模块
- 无法在Lua中加载c dll模块
- 模块化游戏引擎:DLL 循环依赖项