I have written a PostgreSQL extension in C that uses the GSL (GNU Scientific Library) in MSYS2 Environment to calculate factorials. The code compiles successfully,
Acer@LAPTOP-70JE12O9 MSYS /c/Users/Acer/Desktop/DEV/C Dev/sample-GSL
$ make
cc -shared -o xfactorial.dll xfactorial.o -L"C:/Program Files/PostgreSQL/16/lib" -lgsl -lgslcblas -lm -lpostgres
but when I attempt to load the extension in PostgreSQL, I get the following error:
ERROR: could not load library "C:/Program Files/PostgreSQL/16/lib/xfactorial.dll": The specified module could not be found.
SQL state: 58P01
The C code (xfactorial.c):
#include "postgres.h"
#include "fmgr.h"
#include <gsl/gsl_sf_gamma.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
Datum xfactorial(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(xfactorial);
Datum
xfactorial(PG_FUNCTION_ARGS)
{
double n = PG_GETARG_FLOAT8(0); // Get the argument
double result = gsl_sf_gamma(n + 1); // GSL factorial function
PG_RETURN_FLOAT8(result);
}
Makefile:
MODULES = xfactorial
EXTENSION = xfactorial
DATA = xfactorial--1.0.sql
PG_INCLUDE_DIR = "C:/Program Files/PostgreSQL/16/include"
PG_LIB_DIR = "C:/Program Files/PostgreSQL/16/lib"
CFLAGS = -I$(PG_INCLUDE_DIR) -DWIN32
LDFLAGS = -L$(PG_LIB_DIR) -lgsl -lgslcblas -lm -lpostgres
all: $(MODULES).dll
$(MODULES).dll: $(MODULES).o
$(CC) -shared -o $@ $^ $(LDFLAGS)
$(MODULES).o: $(MODULES).c
$(CC) -c $(CFLAGS) -o $@ $^
clean:
rm -f $(MODULES).o $(MODULES).dll
Control File (xfactorial.control):
comment = 'Factorial extension using GSL'
default_version = '1.0'
module_pathname = '$libdir/xfactorial'
relocatable = true
SQL File (xfactorial–1.0.sql):
CREATE FUNCTION xfactorial(integer) RETURNS integer
AS '$libdir/xfactorial.dll', 'xfactorial'
LANGUAGE C STRICT;
PostgreSQL Configuration:
pg_config
BINDIR = C:/PROGRA~1/POSTGR~1/16/bin
LIBDIR = C:/PROGRA~1/POSTGR~1/16/lib
File Locations:
The xfactorial.dll file is located in C:/Program Files/PostgreSQL/16/lib/.
The xfactorial–1.0.sql and xfactorial.control files are located in C:/Program Files/PostgreSQL/16/share/extension/.
Problem:
When I attempt to run:
CREATE EXTENSION xfactorial;
I get the error message:
ERROR: could not load library "C:/Program Files/PostgreSQL/16/lib/xfactorial.dll": The specified module could not be found.
SQL state: 58P01
Question:
- Why is PostgreSQL unable to load the xfactorial.dll shared library, and what can I do to resolve this issue?
- Are there any additional steps or configuration required to make the extension work on Windows with PostgreSQL?
- Is there a problem with how I defined the module_pathname in the .control file or the SQL function?
Attempts to Resolve:
- Verified that the .dll file is in the correct directory (C:/Program Files/PostgreSQL/16/lib/).
- Checked PostgreSQL logs for additional information, but there are no further details.
- Double-checked my extension’s dependencies and the compatibility of PostgreSQL with the GSL library.
- I have tried copying the .dll file into C:/Program Files/PostgreSQL/16/lib/ and ensuring that both the SQL and control files are in the right directory.
AsukaMyFriendThisIsGoodBye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.