Thread: Incompatible library : Missing Magic Block
Hi,
I am trying to write a C function (using Postgres external methods) in windows XP and am using Postgres 8.3.7 version and creating a dll using Microsoft Visual Studio 2005.
However I have included all the required headers and PG_MODULE_MAGIC in my code. (Please find below the example code which I am compiling using MSVC)
_________________________________________________________________
// add.cpp : Defines the entry point for the DLL application.
//
#define _USE_32BIT_TIME_T
#include "postgres.h"
#include "fmgr.h"
//#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
//#endif
PG_FUNCTION_INFO_V1(add_one);
Datum add_one(PG_FUNCTION_ARGS)
{
int32 arg = PG_GETARG_INT32(0);
PG_RETURN_INT32(arg + 1);
}
_____________________________________________________________________
I could compile the code successfully and generate the dll with no errors but whenever I am compiling the below postgres function using that dll, Postgres throws an error
ERROR: incompatible library "C:/Program Files/PostgreSQL/8.3/lib/add.dll": missing magic block
HINT: Extension libraries are required to use the PG_MODULE_MAGIC macro.
Postgres Function
CREATE OR REPLACE FUNCTION add_one(IN int) RETURNS int AS '$libdir/add', 'add_one' LANGUAGE C;
I am using the Postgres 8.3.7 headers to compile the program and have followed the steps to compile the program in MSVC.
Kindly provide your help at the earliest as this is really a show stopper for us.
Many thanks in advance
Ambarish Bhattacharya
Ambarsih, > // add.cpp : Defines the entry point for the DLL application. are you sure that you using the C-Compiler and not the c++ compiler? As much as I know about defaults, that will be a C++ compiled function, which is something totally different then a C compiled function. (and, to my knowledge, a C++ function is not easily callable by PostgreSQL) Harald -- GHUM Harald Massa persuadere et programmare Harald Armin Massa Spielberger Straße 49 70435 Stuttgart 0173/9409607 no fx, no carrier pigeon - %s is too gigantic of an industry to bend to the whims of reality
Massa, Harald Armin wrote: > Ambarsih, > >> // add.cpp : Defines the entry point for the DLL application. > > are you sure that you using the C-Compiler and not the c++ compiler? > As much as I know about defaults, that will be a C++ compiled > function, which is something totally different then a C compiled > function. (and, to my knowledge, a C++ function is not easily callable > by PostgreSQL) So long as the function uses C linkage (extern "C") it'll be callable directly from C++ using dlopen(), LoadLibrary, etc. A C++ function is callable from C via function pointer even if it has C++ linkage, though the C calling convention declared must handle stack management, parameter passing and return value popping in the same way the C++ calling convention on that compiler does. That means that it's unsafe to call a __thiscall C++ member function from C, but otherwise it's generally fine so long as the C and C++ compilers default to the same calling convention or you explicitly specify it on both ends. -- Craig Ringer
On Fri, Oct 23, 2009 at 7:02 AM, Craig Ringer <craig@postnewspapers.com.au> wrote: > Massa, Harald Armin wrote: >> Ambarsih, >> >>> // add.cpp : Defines the entry point for the DLL application. >> >> are you sure that you using the C-Compiler and not the c++ compiler? >> As much as I know about defaults, that will be a C++ compiled >> function, which is something totally different then a C compiled >> function. (and, to my knowledge, a C++ function is not easily callable >> by PostgreSQL) > > So long as the function uses C linkage (extern "C") it'll be callable > directly from C++ using dlopen(), LoadLibrary, etc. > > A C++ function is callable from C via function pointer even if it has > C++ linkage, though the C calling convention declared must handle stack > management, parameter passing and return value popping in the same way > the C++ calling convention on that compiler does. That means that it's > unsafe to call a __thiscall C++ member function from C, but otherwise > it's generally fine so long as the C and C++ compilers default to the > same calling convention or you explicitly specify it on both ends. exactly...also c++ exceptions are incompatible with postgres exceptions iirc, and it's highly advisable to avoid using threads.. also there are a couple of keyword conflicts that are annoying (those may have been fixed). those gotchas aside, putting c++ in the backend is trivially done if you understand difference between c and c++ linkage. merlin
Hello, I'm new to PostgreSQL and wonder if anyone can help. I'm creating an after-update-trigger for all tables, which copy the old records to a backup table. In my test, the table with this trigger has only two columns - one BIGINT and one VARCHAR. Also I'd like to pass the backup table's name through a parameter because I may reuse this function for more than one backup tables. ===== CREATE OR REPLACE FUNCTION cp_tbl() RETURNS TRIGGER AS $proc$ BEGIN EXECUTE 'INSERT INTO ' || TG_ARGV[0] || ' SELECT ' || OLD; RETURN NEW; END; $proc$ LANGUAGE plpgsql; ===== At run-time, it prompts an error: ==== ERROR: column "beginning" does not exist LINE 1: INSERT INTO test_log SELECT (1,BEGINNING) ^ QUERY: INSERT INTO test_log SELECT (1,BEGINNING) CONTEXT: PL/pgSQL function "cp_tbl" line 2 at EXECUTE statement ==== "beginning" is the actual data I stored in the second column of the table. I think the issue is related to the use of OLD in the EXECUTE statement. Does anyone have any idea how to fix it? Many thanks!! Nim
On Fri, Oct 23, 2009 at 11:27:11AM -0400, Nim Li wrote: > CREATE OR REPLACE FUNCTION cp_tbl() RETURNS TRIGGER AS $proc$ > BEGIN > EXECUTE 'INSERT INTO ' || > TG_ARGV[0] || > ' SELECT ' || > OLD; > RETURN NEW; > END; > $proc$ LANGUAGE plpgsql; > > At run-time, it prompts an error: > Does anyone have any idea how to fix it? You need to quote your literals! :) The details are a bit fiddly, but the following thread covered similar things recently: http://archives.postgresql.org/pgsql-general/2009-09/msg01176.php -- Sam http://samason.me.uk/
Thank you Sam!! The code in the posting solves my issue. :) Nim On 10/23/2009 12:07 PM, Sam Mason wrote: > On Fri, Oct 23, 2009 at 11:27:11AM -0400, Nim Li wrote: >> CREATE OR REPLACE FUNCTION cp_tbl() RETURNS TRIGGER AS $proc$ >> BEGIN >> EXECUTE 'INSERT INTO ' || >> TG_ARGV[0] || >> ' SELECT ' || >> OLD; >> RETURN NEW; >> END; >> $proc$ LANGUAGE plpgsql; >> >> At run-time, it prompts an error: > >> Does anyone have any idea how to fix it? > > You need to quote your literals! :) The details are a bit fiddly, but > the following thread covered similar things recently: > > http://archives.postgresql.org/pgsql-general/2009-09/msg01176.php >