Thread

  1. postgres functions and C++

    Vladimir Zolotykh <gsmith@eurocom.od.ua> — 2000-10-04T08:59:06Z

    Hello all,
    
    Please help me with
    create function days_in_month(int4, int4, int4) returns int4
    as '/tmp/days.so' language 'c';
    
    Can I write this function days_in_month in C++ ?
    I've did the following:
    
    extern "C" {
    int days_in_month(int year, int mo, int day); 
    } 
    
    extern "C" { 
    int days_in_month(int year, int mo, int day) { 
    ═══ return 13;
    } 
    } 
    
    Compile with
      $ g++ -I/usr/local/qt/include -o days.so -shared days.cpp
    
    then (as user postgres)
      my=> create function days_in_month(int4, int4, int4) returns int4
      my=> as '/tmp/days.so' language 'c';
      CREATE
      
    I've tried to use new function
      my=> select days_in_month(3, 3, 3);
      ERROR:═ Can't find function days_in_month in file /tmp/days.so
      my=>
      
    What is wrong ?
    I'm using postgres 6.4 under RedHat.
    Any help will be appreciated.
    
    -- 
    Best regards,
     Vladimir                          mailto:gsmith@eurocom.od.ua
    
    
    
    
  2. Re: postgres functions and C++

    Tom Lane <tgl@sss.pgh.pa.us> — 2000-10-04T15:29:48Z

    "Vladimir V. Zolotych" <gsmith@eurocom.od.ua> writes:
    >   my=> select days_in_month(3, 3, 3);
    >   ERROR: Can't find function days_in_month in file /tmp/days.so
    
    Try using 'nm' to see what symbol name is actually being exported
    from the .so file.  I suspect that despite your use of extern "C",
    your C++ compiler is being uncooperative and is naming the function
    in some strange fashion at the link level.
    
    There is an option in CREATE FUNCTION to specify the link symbol
    name separately from the SQL name of the function, so if you can't
    get your compiler to play nice you could still get it to work by
    quoting whatever nm tells you...
    
    			regards, tom lane
    
    
  3. Re: postgres functions and C++

    Christof Petig <christof.petig@wtal.de> — 2000-10-07T21:19:23Z

    "Vladimir V. Zolotych" wrote:
    
    > Hello all,
    >
    > Compile with
    >   $ g++ -I/usr/local/qt/include -o days.so -shared days.cpp
    
    IIRC you have to compile as days.o and then link to .so:
    g++ .... -o days.o
    ld -shared -o days.so days.o
    
    I don't know if the compiler gets it right in one line. It might.
    
    Christof