Thread: building a c function

building a c function

From
Stuart McGraw
Date:
Hello all,

I know this is a very elementary question, but my excuse
is I have not programmed in C or written makefiles for
15+ years...

I am trying to write a C-language function, call it my_func.
But I also have a my_subs.o that does the heavy lifting
for my_func.c.  My makefile is:

MODULES = my_func
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
OBJS = my_subs.o    # Is this right?

But I see no indication in the output that my_subs.o is
being used:

  gcc -O2 -g ... -fpic -shared -o my_func.so my_func.o

How do I tell the makefile to include my_subs.o when building
the shared lib?


Re: building a c function

From
Tom Lane
Date:
Stuart McGraw <smcg2297@frii.com> writes:
> MODULES = my_func
> PG_CONFIG = pg_config
> PGXS := $(shell $(PG_CONFIG) --pgxs)
> include $(PGXS)
> OBJS = my_subs.o    # Is this right?

Not entirely certain, but I think you need to set all the variables
*before* including $(PGXS), so ordering above is no good.  Also I
believe you need to use MODULE_big not MODULES if you intend to link
additional .o files beyond the one corresponding to the module name.
Per the comments in pgxs.mk:

# Use the following layout for your Makefile:
#
#   [variable assignments, see below]
#   [custom rules, rarely necessary]
#
#   PG_CONFIG = pg_config
#   PGXS := $(shell $(PG_CONFIG) --pgxs)
#   include $(PGXS)
#
# Set one of these three variables to specify what is built:
#
#   MODULES -- list of shared objects to be built from source files with
#     same stem (do not include suffix in this list)
#   MODULE_big -- a shared object to build from multiple source files
#     (list object files in OBJS)
#   PROGRAM -- a binary program to build (list object files in OBJS)
#
# The following variables can also be set:
#
#   MODULEDIR -- subdirectory into which DATA and DOCS files should be
#     installed (if not set, default is "contrib")
#   DATA -- random files to install into $PREFIX/share/$MODULEDIR
#   DATA_built -- random files to install into $PREFIX/share/$MODULEDIR,
#     which need to be built first
#   DATA_TSEARCH -- random files to install into $PREFIX/share/tsearch_data
#   DOCS -- random files to install under $PREFIX/doc/$MODULEDIR
#   SCRIPTS -- script files (not binaries) to install into $PREFIX/bin
#   SCRIPTS_built -- script files (not binaries) to install into $PREFIX/bin,
#     which need to be built first
#   REGRESS -- list of regression test cases (without suffix)
#   EXTRA_CLEAN -- extra files to remove in 'make clean'
#   PG_CPPFLAGS -- will be added to CPPFLAGS
#   PG_LIBS -- will be added to PROGRAM link line
#   SHLIB_LINK -- will be added to MODULE_big link line
#   PG_CONFIG -- path to pg_config program for the PostgreSQL installation
#     to build against (typically just "pg_config" to use the first one in
#     your PATH)
#
# Better look at some of the existing uses for examples...

            regards, tom lane

Re: building a c function

From
Carsten Kropf
Date:
Hello,
I think, what you probably want to do is something like that:
MODULE_big = my_funcs
OBJS = myfunc.o mysubs.o
PG_CONFIG=/usr/local/pgsql/bin/pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

Then you will get a shared library called "my_funcs" which includes both of your *.o s and will be deployed to postgres lib dir whenever you call "install" on the makefile.

Best regards
Carsten Kropf
Am 17.03.2010 um 19:27 schrieb Stuart McGraw:

Hello all,

I know this is a very elementary question, but my excuse
is I have not programmed in C or written makefiles for
15+ years...

I am trying to write a C-language function, call it my_func.  
But I also have a my_subs.o that does the heavy lifting
for my_func.c.  My makefile is:

MODULES = my_func
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
OBJS = my_subs.o # Is this right?

But I see no indication in the output that my_subs.o is
being used:

 gcc -O2 -g ... -fpic -shared -o my_func.so my_func.o

How do I tell the makefile to include my_subs.o when building
the shared lib?  


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

Re: building a c function

From
Stuart McGraw
Date:
On 03/17/2010 01:56 PM, Carsten Kropf wrote:
> I think, what you probably want to do is something like that:
> MODULE_big = my_funcs
> OBJS = myfunc.o mysubs.o
> PG_CONFIG=/usr/local/pgsql/bin/pg_config
> PGXS := $(shell $(PG_CONFIG) --pgxs)
> include $(PGXS)
>
> Then you will get a shared library called "my_funcs" which includes both
> of your *.o s and will be deployed to postgres lib dir whenever you call
> "install" on the makefile.

Thanks for the answer Tom and Carsten.  It worked
fine once I figured out that MODULE_big was not
the same as MODULES_big.  :-(