Subject: (?) link failed message?

From: [email-masked]
Date: Tue, 1 Dec 92 18:01:20 -0800

recently several students have been attempting to load .o files into  
a monitor session via the "load" comment. it has been giving back the  
message

Load of file such and such failed!: link failed!

and of course it doesn't load the file.

we're running postgres 4.0.1 running on a sparc-box running SunOS  
4.1.2.

we hope ya'll can help cause we can't find diddly about this problem  
in the reference manuals.

ed murphy

---

From: [email-masked] (Lance Gatrell (303) 977-2052)
Date: Wed, 2 Dec 92 08:53:47 -0800

We ran into the "link failed" problem before.  Here is my previous post
about it:

We recently went through this experience, and so I'm going to share 
with the net what we learned.

Suppose you have two files, foo.c and bar.c that you want to compile
and link into Postgres for your user defined function.  Your function Foo()
is defined in the file foo.c, and it calls the function Bar() in bar.c.
Furthermore, Bar() calls some math routines like cos(), and calls a function
FindBest() which is part of your library /home/gatrell/Libs/libutils.a.

You can accomplish this on a Sun with the following:
	cc -c foo.c
	cc -c bar.c
	ld -r foo.o bar.o -L/home/gatrell/Libs -lutils -o foobar.o

In Postgres, you define your function as:
	define function Foo (language = "c", returntype = int4) arg is
	(emp) as "/home/gatrell/foobar.o"

The important point here is that in the "ld" command you did NOT link
in the math library (-lm), even though Bar() calls cos().  If you do link
in the math library at this point, your function definition/load will fail
with the error
	"...... link failed"
(No explanation of why it failed if you are running from the monitor (but
more about that later)).

The reason that the link failed is that when Postgres loads in a user's
function, Postgres specifies the -lm option.  Apparently, many dot-o files
from libm.a have been previously linked into the Postgres executable, and
if you include the "-lm" option in the linking of YOUR dot-o files, 
you will link in some of the dot-o files from libm.a into foobar.o, so
that when Postgres links in foobar.o, there are multiple definitions of
math functions, causing the load to fail.

There are two lessons learned here:
1.  When Postgres loads a function definition and the link fails, it may 
have failed because not all of the required function definitions were 
loaded (ie, the definition of FindBest() from the library libutils.a, if
you forgot to link it in), or, you may have multiple definitions of the 
same function (from linking in libm.a twice like we did, or for any other 
reason).

2.  If you run into a problem with the loading or execution of a user's
function while running from the monitor (of course, no error messages
from the "ld" or the user's function will be output), I would suggest
doing the operation in the Postgres backend (do not append "\g" to commands
in the interactive Postgres backend).  Any error messages are then visible
(that's how we tracked down the problem with the "-lm" in the "ld" command).
Printfs are also visible.  (In other words, execute postgres in your Unix
shell to debug.)

Hope this helps,

Lance B. Gatrell
[email-masked]

Point number 2 should be the most helpful in tracking down your particular
problem.