BUG #14215: External C function link failed on Mac OSX

Started by Max Fomichevalmost 10 years ago2 messagesbugs
Jump to latest
#1Max Fomichev
max.fomitchev@gmail.com

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

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Max Fomichev (#1)
Re: BUG #14215: External C function link failed on Mac OSX

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