From owner-freebsd-hackers Wed Feb 4 09:14:17 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id JAA19282 for hackers-outgoing; Wed, 4 Feb 1998 09:14:17 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from austin.polstra.com (austin.polstra.com [206.213.73.10]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id JAA19055 for ; Wed, 4 Feb 1998 09:13:53 -0800 (PST) (envelope-from jdp@austin.polstra.com) Received: from austin.polstra.com (jdp@localhost) by austin.polstra.com (8.8.8/8.8.8) with ESMTP id JAA03080; Wed, 4 Feb 1998 09:13:47 -0800 (PST) (envelope-from jdp) Message-Id: <199802041713.JAA03080@austin.polstra.com> To: xavier@stlnet.com Subject: Re: Problems linking shared libraries with ld In-Reply-To: <34D76A00.3F9F4818@stlnet.com> References: <34D76A00.3F9F4818@stlnet.com> Organization: Polstra & Co., Seattle, WA Cc: hackers@FreeBSD.ORG Date: Wed, 04 Feb 1998 09:13:47 -0800 From: John Polstra Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG X-To-Unsubscribe: mail to majordomo@FreeBSD.org "unsubscribe hackers" In article <34D76A00.3F9F4818@stlnet.com>, Jon E. Kump wrote: > I am writing some code that I am linking in 11 shared libraries and one > static library. When ld goes to link the code I get an error from ld. > > ld: No reference to __DYNAMIC > > This is my make file that i am using. This program is a little 20 line > example. > > CC = gcc > CFLAGS = -g -funroll-loops -Wall -pipe -ansi > LDFLAGS = -Bdynamic > CPPFLAGS = > INCLUDES = -I. -I/usr/X11R6/include -I/usr/local/include > LIBDIR = -L/usr/X11R6/lib -L/usr/local/lib > LIBS = -lXm -lXpm -lXmu -lXt -lXext -lX11 -lXmHTML -lSM -lICE -ljpeg > -lpng -lz -lm > LOADLIBES = $(LIBDIR) $(LIBS) > > OBJS = autosize_html.o > > all: $(OBJS) autosize_html > > autosize_html.o: autosize_html.c > $(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) -c $< > > autosize_html: $(OBJS) > ld $(LDFLAGS) $(LOADLIBES) -o autosize_html $(OBJS) There are several problems with this makefile. First, all of your libraries must be listed at the _end_ of the linker command, i.e., _after_ all of the object files. Otherwise, the libraries will ignored, more or less. Second, all dynamic executables have to be linked with "/usr/lib/crt0.o" as the very first object file. The fact that you didn't do that is the source of the "No reference to __DYNAMIC" diagnostic. But the real problem is that you shouldn't be using "ld" directly for linking. Use "cc" instead, like this: $(CC) -o autosize_html $(OBJS) $(LDFLAGS) $(LOADLIBES) The exact command for linking an executable is different on practically every platform in existence. The "cc" command knows the idiosyncrasies of your platform, and it does the right thing. It's a much more portable way to build programs than using "ld" directly. John -- John Polstra jdp@polstra.com John D. Polstra & Co., Inc. Seattle, Washington USA "Self-knowledge is always bad news." -- John Barth