From owner-freebsd-hackers@FreeBSD.ORG Wed Dec 20 11:29:36 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 C635F16A407 for ; Wed, 20 Dec 2006 11:29:36 +0000 (UTC) (envelope-from artifact.one@googlemail.com) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.188]) by mx1.FreeBSD.org (Postfix) with ESMTP id B735543C9F for ; Wed, 20 Dec 2006 11:29:35 +0000 (GMT) (envelope-from artifact.one@googlemail.com) Received: by nf-out-0910.google.com with SMTP id x37so2457920nfc for ; Wed, 20 Dec 2006 03:29:34 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=googlemail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=fOcYQimDZYrfxvus07b6AaiCzKQKLwQJ9tAzvqiG6UwZW714PDxEnLJb0+KSvEJzfBhv9G53Kufx4yYi/3VEZVCLkAY35miB5oDoDliD9kVYHIg973Ld5A4ArhXjxAUIxHomduMXmKv7KVyZ22JluoXAdODzZWvzUJdvITgcyK8= Received: by 10.48.216.8 with SMTP id o8mr8872634nfg.1166612510804; Wed, 20 Dec 2006 03:01:50 -0800 (PST) Received: by 10.49.17.19 with HTTP; Wed, 20 Dec 2006 03:01:50 -0800 (PST) Message-ID: <8e96a0b90612200301l467b2688j157071f205685e7@mail.gmail.com> Date: Wed, 20 Dec 2006 11:01:50 +0000 From: "mal content" To: freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: 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 11:29:36 -0000 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'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'. Any thoughts? MC