From owner-freebsd-hackers@FreeBSD.ORG Wed Dec 20 18:56:22 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 0218D16A40F for ; Wed, 20 Dec 2006 18:56:22 +0000 (UTC) (envelope-from mwm-keyword-freebsdhackers2.e313df@mired.org) Received: from mired.org (vpn.mired.org [66.92.153.74]) by mx1.FreeBSD.org (Postfix) with SMTP id C2D5143CB0 for ; Wed, 20 Dec 2006 18:53:08 +0000 (GMT) (envelope-from mwm-keyword-freebsdhackers2.e313df@mired.org) Received: (qmail 9565 invoked by uid 1001); 20 Dec 2006 18:53:11 -0000 Received: by bhuda.mired.org (tmda-sendmail, from uid 1001); Wed, 20 Dec 2006 13:53:11 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <17801.34454.993122.932654@bhuda.mired.org> Date: Wed, 20 Dec 2006 13:53:10 -0500 To: "mal content" In-Reply-To: <8e96a0b90612200301l467b2688j157071f205685e7@mail.gmail.com> References: <8e96a0b90612200301l467b2688j157071f205685e7@mail.gmail.com> X-Mailer: VM 7.17 under 21.4 (patch 19) "Constant Variable" XEmacs Lucid X-Primary-Address: mwm@mired.org X-face: "5Mnwy%?j>IIV\)A=):rjWL~NB2aH[}Yq8Z=u~vJ`"(,&SiLvbbz2W`; h9L,Yg`+vb1>RG% *h+%X^n0EZd>TM8_IB;a8F?(Fb"lw'IgCoyM.[Lg#r\ X-Delivery-Agent: TMDA/1.1.5 (Fettercairn) From: Mike Meyer Cc: freebsd-hackers@freebsd.org Subject: Re: Linking static libraries with '-l' X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Dec 2006 18:56:22 -0000 In <8e96a0b90612200301l467b2688j157071f205685e7@mail.gmail.com>, mal content typed: > Hi. > > So, if I want to link to the shared library /usr/local/libxyz.so, I > simply add '-lxyz' > to my program link commands. But what if I want to link to the equivalent > static library? > > The GCC manual says: > > -static > On systems that support dynamic linking, this prevents linking > with the shared libraries. On other systems, this option has no > effect. > > This is unsuitable as it will obviously compile in EVERYTHING statically, > including the system libc. > > How come there's no (obvious) portable way to link to static libraries? The > '-l' method is "portable" because the Makefile doesn't need to know the suffix > used for shared libraries on any particular platform (so on FreeBSD, dylib on > Darwin, sl on HP-UX, dll on Windows, and most likely more). However, to link > against static libs, the only option appears to be to do: > > cc -o myprog myprog.o /usr/local/libxyz.a > > The static library must be specified by full path, using the '.a' > suffix - obviously > contains potential portability problems in the event of a platform not using > the '.a' suffix (although I've not actually seen any to date) - > because otherwise > the existence of libxyz.so will cause a program that uses '-l' to link against > the dynamic library instead of the static. > > Is there a better way? I'd try using ld with the "-r -static" flags to do a partial link with just the stuff you want statically linked, then a second ld (or maybe cc?) step to create the executable with everything else linked in shared. I haven't tried it, so I don't know that it will work. You may need to add extra flags to get ld to not include things in the static link as well. Check the ld man page for lots more info. http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more information.