Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > PostgreSQL > C-language external functions

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-08, 08:12
brbrbr brbrbr is offline
Registered User
 
Join Date: Jan 2008
Location: Moscow
Posts: 8
C-language external functions

Hello

I'm working with PostgreSQL under Windows operationg system, and now I need to write some external C-language functionf for PostgreSQL. As I read in documentation it is possible to compile .so shared library with function and export it from .so. But as I also read it is possible only in linux/unix system using gcc, not in windows.

Is it possible to export C-language function to PostgreSQL in Windows? Would be nice if it also possible using Visual Studio

I'm sorry if such questions were already asked, but I didn't find them.

Last edited by brbrbr : 01-11-08 at 08:21.
Reply With Quote
  #2 (permalink)  
Old 01-14-08, 04:09
brbrbr brbrbr is offline
Registered User
 
Join Date: Jan 2008
Location: Moscow
Posts: 8
I read on some forum that it's possible in Windows to compile .dll that contains needed c-language function.

I tested it on a function that returns 1. Here is mydll.c listing:
Code:
#ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif #include "postgres.h" #include "commands/trigger.h" /* ... and triggers */ PG_FUNCTION_INFO_V1(trigf); Datum trigf(PG_FUNCTION_ARGS) { PG_RETURN_INT32(1); }

I also created .def file
Code:
EXPORTS trigf

then I'm trying to import the functions from .dll
Code:
CREATE FUNCTION func_name() RETURNS integer AS 'c:/path/mydll.dll' LANGUAGE 'C';

an error appears
Code:
ERROR: incompatible library "c:/path/mydll.dll": missing magic block SQL state: XX000 Hint: Extension libraries are required to use the PG_MODULE_MAGIC macro.

as I read somewhere it is neccessary to include
Code:
#ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif
code to source file to prevent the error, but it didn't helps.

Could someone help me with this problem? Or, may be, somebody already faced and solved it?

Last edited by brbrbr : 01-14-08 at 04:14.
Reply With Quote
  #3 (permalink)  
Old 01-17-08, 09:39
brbrbr brbrbr is offline
Registered User
 
Join Date: Jan 2008
Location: Moscow
Posts: 8
The problem is solved.

It occurs that Visual Studio were using PostgreSQL 8.1.4 libraries instead of 8.2.4, although function was testing in PostgreSQL 8.2.4.
Reply With Quote
  #4 (permalink)  
Old 09-06-08, 19:03
prduque prduque is offline
Registered User
 
Join Date: Sep 2008
Posts: 2
same problem, please some help needed

I'm having the same problem in creating an external C function compiled with Visual Studio C++.

I'm using PostgreSQL 8.3.3, and when i compile in the IDE it makes the dll, but i receive missing PG_MODULE_MAGIC error.

If I try to compile in command-line with
CL -Ipath_to_pg_serve_include myfunc.cpp
it says missing "libintl.h", as well as some type redefinition errors:

C:\Documents and Settings\pduque.WOLVERINE\My Documents\MyFuncpg>cl /I"C:\Program Files\PostgreSQL\8.3\include\server" myfunc.cpp

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

myfunc.cpp
c:\program files\postgresql\8.3\include\server\pg_config_os.h (188) : error C2011
: 'timezone' : 'struct' type redefinition
c:\program files\postgresql\8.3\include\server\pg_config_os.h (188) : see
declaration of 'timezone'
c:\program files\postgresql\8.3\include\server\pg_config_os.h (197) : error C2011
: 'itimerval' : 'struct' type redefinition
c:\program files\postgresql\8.3\include\server\pg_config_os.h (197) : see
declaration of 'itimerval'
c:\program files\postgresql\8.3\include\server\c.h(97) : fatal error C1083: Cann
ot open include file: 'libintl.h': No such file or directory

Reply With Quote
  #5 (permalink)  
Old 09-06-08, 22:22
prduque prduque is offline
Registered User
 
Join Date: Sep 2008
Posts: 2
It was a bug with PGDLLIMPORT.
corrected it, and now I can build the dll ok.
But it still says missing Magic number
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On