Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2008
    Location
    Moscow
    Posts
    8

    Unanswered: 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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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


  5. #5
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •