From owner-freebsd-hackers@FreeBSD.ORG Wed Dec 20 16:12:44 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 5D84A16A403 for ; Wed, 20 Dec 2006 16:12:44 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5B1E643CC9 for ; Wed, 20 Dec 2006 16:12:21 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.13.6/8.13.8) id kBKFb10L008913; Wed, 20 Dec 2006 09:37:01 -0600 (CST) (envelope-from dan) Date: Wed, 20 Dec 2006 09:37:01 -0600 From: Dan Nelson To: mal content Message-ID: <20061220153700.GD41207@dan.emsphone.com> References: <8e96a0b90612200301l467b2688j157071f205685e7@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8e96a0b90612200301l467b2688j157071f205685e7@mail.gmail.com> X-OS: FreeBSD 6.2-PRERELEASE X-message-flag: Outlook Error User-Agent: Mutt/1.5.13 (2006-08-11) 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 16:12:44 -0000 In the last episode (Dec 20), mal content said: > 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? One method is to pass -Bstatic and -Bdynamic to the linker at appropriate places: ${CC} ${CFLAGS} ${LDFLAGS} -o myprogram myprogram.o -Wl,-Bstatic -lxyz -Wl,-Bdynamic That line will pull in libxyz.a while trying to use shared libraries for everything else. The drawbacks are: 1) if for some reason you want to link that binary statically, you can't just add a LDFLAGS+=-static to the Makefile; you have to remove all instances of -Wl,-Bdynamic; 2) it's not a standard option (-Wl and -B are supported by Solaris and GNU cc and ld, but not AIX), so it's no more portable than determining the static library's filename and linking to it directly. > I've not tried it, but I think this might work: > > /usr/local/lib/libxyz.so > /usr/local/lib-static/libxyz.a > > That way, a program should be able to specify: > > cc -o myprog myprog.o -L/usr/local/lib -lxyz.so -L/usr/local/lib-static -labc > > ...and get the dynamic 'libxyz.so' and the static 'libabc.a'. -L adds paths to the end of the search list, so if there's a /usr/local/lib/libabc.so, the linker will use that. -- Dan Nelson dnelson@allantgroup.com