LIBS or LDLIBS ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

LIBS or LDLIBS ?

Sébastien Hinderer
Dear autoconf and make users,

The status of the LIBS and LDLIBS variables is unclear to me.

Do both of them have a conventional meanings, or are they just
alternatives for the same thing, namely giving libraries that should be
passed to the ld linker?

./configure --help refers to LIBS but in make's manual it's LDLIBS which
is mentionned and the GNU coding standard at
https://www.gnu.org/prep/standards/html_node/index.html does not help...

Thanks in advance for any advice,

Sébastien.

Reply | Threaded
Open this post in threaded view
|

Re: LIBS or LDLIBS ?

Kaz Kylheku (gmake)
On 2021-01-14 02:45, Sébastien Hinderer wrote:
> Dear autoconf and make users,

I suspect mailing list cross-posting doesn't work well in an
age in which mailing lists don't accept postings from non-subscribers
due to the spam problem.

Your question reaches people who are subscribed only to one
of the lists, and can only post to that one.

> The status of the LIBS and LDLIBS variables is unclear to me.
>
> Do both of them have a conventional meanings, or are they just
> alternatives for the same thing, namely giving libraries that should be
> passed to the ld linker?
>
> ./configure --help refers to LIBS but in make's manual it's LDLIBS
> which
> is mentionned and the GNU coding standard at
> https://www.gnu.org/prep/standards/html_node/index.html does not
> help...

The GNU make manual is clear about this:

'LDFLAGS'
      Extra flags to give to compilers when they are supposed to invoke
      the linker, 'ld', such as '-L'.  Libraries ('-lfoo') should be
      added to the 'LDLIBS' variable instead.

'LDLIBS'
      Library flags or names given to compilers when they are supposed to
      invoke the linker, 'ld'.  'LOADLIBES' is a deprecated (but still
      supported) alternative to 'LDLIBS'.  Non-library linker flags, such
      as '-L', should go in the 'LDFLAGS' variable.

The built-in rules for linking reference these variables. If your
program uses the built-in rules, then it doesn't have to do anything
in order to support these variables.

If you write your own linking recipe from scratch, it should mimic
the built-in one and refer to LDFLAGS and LDLIBS.

The main idea is that these variables (along with CFLAGS and some
others)
belong to the user who builds the program, who may need to use them
to pass additional compiler options.

This means that your build system reacts and incorporates the values
of these variables if they are set in the environment or on the make
command line, but does not set them itself.

Use your own variables to specify everything that is internally
necessary for your program to build and work.

Do not do the following:

    CFLAGS = -std=c99 -D_POSIX_SOURCE -O2 # program's needed flags
    LDLIBS = -lfoo -lbar  # program's needed libs

The problem is that if the user wants to add some extra flag,
they have to replicate all of your required flags that are specific
to your program.

These variables are typically globally set by operating system distro
builds. Programs which hijack the variables for specifying necessary
options and libraries cause work for package and distro maintainers.

For instance, a distro might use CFLAGS to select a consistent ABI
across the entire system, or to select code generation for a
a particular instruction set. Or LDLIBS to inject some common
library into every program. I suspect that is rare, but distro-wide
use of LDFLAGS is very common.

I see I'm missing support for LDLIBS in some of my programs,
but I've not received any reports about this.

LIBS does appear in the GNU Make manual, in a 1991-dated
"complex Makefile example" from GNU tar which doesn't reflect current
good practices.


Reply | Threaded
Open this post in threaded view
|

Re: LIBS or LDLIBS ?

Sébastien Hinderer
Hi,

Kaz Kylheku (gmake) (2021/01/14 03:37 -0800):
> On 2021-01-14 02:45, Sébastien Hinderer wrote:
> > Dear autoconf and make users,
>
> I suspect mailing list cross-posting doesn't work well in an
> age in which mailing lists don't accept postings from non-subscribers
> due to the spam problem.

I'm subscribed to both lists, so I was expecting my mail to reach both
of them. I did realise that the responses of people on one list wouldn't
be seen on the other one, if thosepeople were not subscribed to both
lists.

> > The status of the LIBS and LDLIBS variables is unclear to me.
> >
> > Do both of them have a conventional meanings, or are they just
> > alternatives for the same thing, namely giving libraries that should be
> > passed to the ld linker?
> >
> > ./configure --help refers to LIBS but in make's manual it's LDLIBS which
> > is mentionned and the GNU coding standard at
> > https://www.gnu.org/prep/standards/html_node/index.html does not help...
>
> The GNU make manual is clear about this:

[...]

Did you read my question properly?

I mentionned the *LIBS* variable. Try to run ./configure --help, you'll
see what I mean.

My question is: between LIBS and LDLIBS, which one is a build system
supposed to honour?

Sébastien.

Reply | Threaded
Open this post in threaded view
|

Re: LIBS or LDLIBS ?

Paul Smith-20
On Thu, 2021-01-14 at 12:56 +0100, Sébastien Hinderer wrote:
> My question is: between LIBS and LDLIBS, which one is a build system
> supposed to honour?

"Supposed to" according to whom?

The GNU make manual, and the built-in GNU make rules, all use LDLIBS.
 There is no mention of LIBS there.

So, if you're following basic GNU make guidelines and you want to use,
or emulate, the built-in rules for GNU make you should use LDLIBS.

If you're using other build environments they may have other rules or
traditions.  Insofar as you're using those environments, you may want
to follow their traditional names.

With respect to autoconf, libraries that are detected by configure
scripts as being required and present on the system are added to LIBS so if you're writing a makefile to be used with autoconf (say you're writing a Makefile.in) then you will want to include $(LIBS) in your link line, so that the configure script can inform your makefile of which libraries to link.

You may also want to add $(LDLIBS) as well: perhaps this would be
useful for users who needed to add extra link libraries that the
configure script didn't find: it could be set on the make command line
for example.


Reply | Threaded
Open this post in threaded view
|

Re: LIBS or LDLIBS ?

Sébastien Hinderer
Hi Paul, many thanks for your response.

Paul Smith (2021/01/14 08:56 -0500):
> On Thu, 2021-01-14 at 12:56 +0100, Sébastien Hinderer wrote:
> > My question is: between LIBS and LDLIBS, which one is a build system
> > supposed to honour?
>
> "Supposed to" according to whom?

Is there a standard or convention as there is for CFLAGS, say.

> The GNU make manual, and the built-in GNU make rules, all use LDLIBS.
>  There is no mention of LIBS there.
>
> So, if you're following basic GNU make guidelines and you want to use,
> or emulate, the built-in rules for GNU make you should use LDLIBS.
>
> If you're using other build environments they may have other rules or
> traditions.  Insofar as you're using those environments, you may want
> to follow their traditional names.
>
> With respect to autoconf, libraries that are detected by configure
> scripts as being required and present on the system are added to LIBS
> so if you're writing a makefile to be used with autoconf (say you're
> writing a Makefile.in) then you will want to include $(LIBS) in your
> link line, so that the configure script can inform your makefile of
> which libraries to link.

That's my situation, yes. But what if the user wants to pass additional
libraries at configure-time? Shoudl configure take such libraries from
LIBS or LDLIBS? Or both?

> You may also want to add $(LDLIBS) as well: perhaps this would be
> useful for users who needed to add extra link libraries that the
> configure script didn't find: it could be set on the make command line
> for example.

Well if configure had failed to detect, say, -lfoo, then I think I'd
prefer to re-run it with a LIBS=-lfoo (or LDLIBS=-lfoo, that's my whole
question) as arguemnt than running just make LDLIBS=-lfoo, because
running configure has the advantage of storing the value in the build
system, which make does not do as far as I know, at least not out of the
box.

Sébastien.