undefined reference to `palloc'

I am trying to compile and link a function example from PostgreSQL manual:

#include "postgres.h"   /* this includes directly palloc.h */
#include "fmgr.h"
#include "utils/geo_decls.h"    /* point type */

PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(makepoint);
Datum makepoint(PG_FUNCTION_ARGS)
{
   Point *pointx = PG_GETARG_POINT_P(0);
   Point *pointy = PG_GETARG_POINT_P(1);
   Point *new_point = (Point *) palloc(sizeof(Point));
   new_point->x = pointx->x;
   new_point->y = pointy->y;
   PG_RETURN_POINT_P(new_point);
}

I have compiled it with command:

cc -c -fPIC -DHAVE_LONG_LONG_INT_64 -I/C:/applications/PostgreSQL/11.2.2/include/server -I/C:/applications/PostgreSQL/11.2.2/include/server/port/win32 ../src/makepoint.c

apparently it compiles finely, but when linking:

cc -shared -o makepoint.so makepoint.o

The following error arises:

makepoint.o:makepoint.c:(.text+0x44): undefined reference to `palloc'
collect2.exe: error: ld returned 1 exit status

The compiler is

cc.exe (Rev1, Built by MSYS2 project) 8.2.1 20181207
installed in MSYS on Windows 10.

(The same error arises with gcc or g++) Please, help me understand what I mistakenly do. Thanks in advance.


While you've correctly included the headers needed to compile against the PostgreSQL library, you are not linking any of the shared libraries needed to create the final executable. You should change your final compile command to something like the following for gcc on Linux:

cc -c -fPIC makepoint.c -o makepoint.o
cc -shared --relocatable -o libmakepoint.so makepoint.o
cc -L/usr/local/pgsql/lib -lpq -L. -lmakepoint -o myapp.exe main.c

You will then need to make sure your local directory where libmakepoint.so is located is available to the dynamic linker at runtime by either defining the path in your environment's LD_LIBRARY_PATH, or adding a configuration to ld_config. You can read more about that process here.

Note that your installation directories may differ depending on how you've installed PostgreSQL, as well as your platform, but since you're using MSYS2, you should be able to install pkg-config, and use the various --cflags, --includedir, --libs, and --libdir commands to get the appropriate search directories and names of the libraries to link to. For instance:

pkg-config --libs libpq

will output (depending on your configuration):

-L/usr/local/pgsql/lib -lpq

and you can then add that string into your build command to link against the correct libraries as well as point the compiler to the correct library path.

Finally, you can reference this page for additional build instructions.

Re: Problems using palloc in postgresql user C functions, I've got that much. But when >> I compile and link my library with those statements (using libtool >> and gcc) I get "undefined reference" errors for the following: Every time I execute that postgre tells me that in the .so file there is an undefined symbol called palloc I know this version of Postgresql is outdated but it seems to be the only version working with my project.


You've put the -fPIC option on the wrong stage of building the shared object. It needs to go on the linking stage, not the compilation stage. Like this...

cc -fPIC -shared -o makepoint.so makepoint.o

using palloc in extension functions in c, undefined reference to `CurrentMemoryContext' /tmp/cc0X3Urq.o(.text+0x4e5): In function `readScoreMatrix': : undefined reference to `MemoryContextAlloc' Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.


So, to compile, the following command should be used:

cc -fPIC -DHAVE_LONG_LONG_INT_64 -c ../src/makepoint.c -I/C:/applications/PostgreSQL/12.0/include/server -I/C:/applications/PostgreSQL/12.0/include/server/port/win32

and then to link:

cc -shared -o makepoint.so makepoint.o -L/c/applications/PostgreSQL/12.0/lib -l postgres

Cmake build under mingw64 fails on pc_access.c.obj build � Issue , undefined reference to `palloc' c:/ming64gcc48/projects/postgis/pointcloud/ pointcloud/pgsql/pc_access.c:327: undefined reference to� sherrellbc changed the title Following steps in QuickStart identically results in undefined reference to __wrap__calloc_r when compiling Undefined reference to __wrap__calloc_r when compiling Aug 23, 2016


Can't build under mingw64 � Issue #17 � pgpointcloud/pointcloud , /home/travis/build/postgis/postgis/regress/00-regress-install/lib/postgis-2.2.so: undefined symbol: palloc0. where did this palloc0 thing coming from and why is� Joe, It simply was a misconfiguration made by me. Everything is working as expected now. Sorry for this bug, I'm closing it! ;-)


#2594 (travis failing with undefined palloc) – PostGIS, st_geometry.so: undefined reference to `pq_sendbytes' st_geometry.so: undefined reference to `palloc' st_geometry.so: undefined reference to� Joe Conway wrote: > D. Stimits wrote: > > > A google search shows very little concerning the pg_detoast_datum > > undefined reference link error. It is looking more


st_geometry.so issue | GeoNet, The Esri Community, makepoint.o:makepoint.c:(.text+0x44): undefined reference to `palloc' collect2. exe: error: ld returned 1 exit status. 编译器是 cc.exe (Rev1, Built� undefined reference 'mkl_free_' Has anyone have this problem? Thanks. Hi, a few things: (a) your version is too old. I believe the memory management was introduced