From owner-freebsd-hackers@FreeBSD.ORG Wed Dec 20 18:04:47 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 4EE2016A417 for ; Wed, 20 Dec 2006 18:04:47 +0000 (UTC) (envelope-from zombyfork@gmail.com) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.241]) by mx1.FreeBSD.org (Postfix) with ESMTP id C91E343CA4 for ; Wed, 20 Dec 2006 18:04:19 +0000 (GMT) (envelope-from zombyfork@gmail.com) Received: by an-out-0708.google.com with SMTP id c24so648528ana for ; Wed, 20 Dec 2006 10:03:48 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:references; b=YiGY79zn1seSoy4qjGxaI7Im7BHkNLGEj4ya6s7WqA8RdSq4zYyX7xinVkRoy0j3F993aAYLqwfsESrVfi7goQeFAnvvOAi+iy0fLub/iYBux5dyZf98gVhqi73m4fHU0dwKJ/lWkpTDaJEctnr22XlT/lRYSjKHSwiYH9S7uaY= Received: by 10.78.18.3 with SMTP id 3mr5316053hur.1166637827359; Wed, 20 Dec 2006 10:03:47 -0800 (PST) Received: by 10.78.168.20 with HTTP; Wed, 20 Dec 2006 10:03:47 -0800 (PST) Message-ID: <346a80220612201003y4ae8d288tbaf335cc29af1a38@mail.gmail.com> Date: Wed, 20 Dec 2006 11:03:47 -0700 From: "Coleman Kane" To: "Dan Nelson" In-Reply-To: <20061220153700.GD41207@dan.emsphone.com> MIME-Version: 1.0 References: <8e96a0b90612200301l467b2688j157071f205685e7@mail.gmail.com> <20061220153700.GD41207@dan.emsphone.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: freebsd-hackers@freebsd.org, mal content Subject: Re: Linking static libraries with '-l' X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: cokane@cokane.org 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:04:47 -0000 On 12/20/06, Dan Nelson wrote: > > 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 I believe you can also specify ar archives (what static libs really are is ar(1) archives of .o files with ranlib(1) run across it). For example.... the following code will be linked to libm.so normally: #include #include int main(int argc, char **argv) { double aoi = 12.0; printf("%f\n", fabs(aoi)); } If I would like to link it against shared libm (/usr/lib/libm.so): cc -o mathprog mathprog.c -lm or simply cc -o mathprog mathprog.c (since FreeBSD automatically does the -lm in this case) If I would like to use the static libm (located at /usr/lib/libm.a): cc -o mathprog mathprog.c /usr/lib/libm.a Its not quite as nice as the auto-search behavior, but it allows you to literally specify a static library on the commandline. I believe there might be one caveat in the case where another dynamic library dynamically links in libm.so... mainly an ambiguity of which fabs() is actually called... I am not familiar with how this would be resolved in this case. -- Coleman Kane