pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14215: External C function link failed on Mac OSX
2+ messages / 2 participants
[nested] [flat]

* BUG #14215: External C function link failed on Mac OSX
@ 2016-06-26 09:27  max.fomitchev@gmail.com
  0 siblings, 1 reply; 2+ messages in thread

From: max.fomitchev@gmail.com @ 2016-06-26 09:27 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14215
Logged by:          Max Fomitchev
Email address:      max.fomitchev@gmail.com
PostgreSQL version: 9.5.3
Operating system:   OSX
Description:        

Example code main.cpp:

extern "C" {
    #include <postgres.h>
    #include <fmgr.h>
    #include <utils/array.h>
    #include <utils/lsyscache.h>
    #include <catalog/pg_type.h>

    #ifdef PG_MODULE_MAGIC
        PG_MODULE_MAGIC;
    #endif

    PG_FUNCTION_INFO_V1(psql_nearest);
    Datum psql_nearest(PG_FUNCTION_ARGS) {
        if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
            elog(ERROR, "DOC2VEC: NULL INPUT DATA");
            PG_RETURN_NULL();
        }

        ArrayType *_docVector = PG_GETARG_ARRAYTYPE_P(0);
        Oid elTypeVals = ARR_ELEMTYPE(_docVector);
        if (elTypeVals != FLOAT4OID) {
            elog(ERROR, "DOC2VEC: INVALID INPUT DATA TYPE");
            PG_RETURN_NULL();
        }

        int16 typeLenVals = 0;
        bool typeByValVals = false;
        char typeAlignVals = char(0);
        get_typlenbyvalalign(elTypeVals, &typeLenVals, &typeByValVals,
&typeAlignVals);

        Datum *inputVals;
        bool *nullVals;
        int nVals;
        deconstruct_array(_docVector, elTypeVals, typeLenVals,
typeByValVals, typeAlignVals, &inputVals, &nullVals, &nVals);

        pfree(inputVals);
        pfree(nullVals);

        PG_RETURN_NULL();
    }
}

Compiling:
c++ -I/usr/local/Cellar/postgresql/9.5.3/include/server -fpic -c
./main.cpp

Linking:
c++ -shared -o ttt.dylib main.o 
Undefined symbols for architecture x86_64:
  "_deconstruct_array", referenced from:
      _psql_nearest in main.o
  "_elog_finish", referenced from:
      _psql_nearest in main.o
  "_elog_start", referenced from:
      _psql_nearest in main.o
  "_get_typlenbyvalalign", referenced from:
      _psql_nearest in main.o
  "_pfree", referenced from:
      _psql_nearest in main.o
  "_pg_detoast_datum", referenced from:
      _psql_nearest in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #14215: External C function link failed on Mac OSX
@ 2016-06-26 15:22  Tom Lane <tgl@sss.pgh.pa.us>
  parent: max.fomitchev@gmail.com
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2016-06-26 15:22 UTC (permalink / raw)
  To: max.fomitchev@gmail.com; +Cc: pgsql-bugs

max.fomitchev@gmail.com writes:
> Linking:
> c++ -shared -o ttt.dylib main.o 

This is not a PG bug. Having said that, the above is never going to
work on OS X; it has its own ideas about how to do things.  Instead
of "-shared" you need "-bundle -bundle_loader /path/to/postgres",
and there are some other linker flags that are advisable too.
Also, PG expects the file extension for loadable modules to be .so
even on OSX.

It's usually better to use PGXS to build extensions, instead of
learning such details for yourself:
https://www.postgresql.org/docs/9.5/static/extend-pgxs.html
Or you can crib from one of the extensions in the contrib/
source tree.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2016-06-26 15:22 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-06-26 09:27 BUG #14215: External C function link failed on Mac OSX max.fomitchev@gmail.com
2016-06-26 15:22 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox