Automake passing CFLAGS or CXXFLAGS when linking

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

Automake passing CFLAGS or CXXFLAGS when linking

Grégory Pakosz
Hello,

What's the rationale behind Automake passing CFLAGS or CXXFLAGS when linking?

LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
  $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
  $(AM_LDFLAGS) $(LDFLAGS) -o $@

CXXLINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \
  $(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \
  $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@

In comparison, GNU Make's default linking rule is

.o:
  $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@

where LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)

Regards,
Gregory

Reply | Threaded
Open this post in threaded view
|

Re: Automake passing CFLAGS or CXXFLAGS when linking

Mike Miller
On Tue, Jun 28, 2016 at 19:21:45 +0200, Grégory Pakosz wrote:

> Hello,
>
> What's the rationale behind Automake passing CFLAGS or CXXFLAGS when linking?
>
> LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
>   $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
>   $(AM_LDFLAGS) $(LDFLAGS) -o $@
>
> CXXLINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \
>   $(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \
>   $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
>
> In comparison, GNU Make's default linking rule is
>
> .o:
>   $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
>
> where LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)

I don't know if I can tell you an offical project rationale, but I can
give a couple examples of options that you really do want to appear in
both places, -pthread and -fopenmp.

If automake did not invoke the link stage this way, building with
CFLAGS=-fopenmp but forgetting to include it in LDFLAGS might cause
serious problems.

OTOH asking the compiler to link some object code and including a few
options that have nothing to do with linking should be harmless.

--
mike

Reply | Threaded
Open this post in threaded view
|

Re: Automake passing CFLAGS or CXXFLAGS when linking

Bob Friesenhahn
In reply to this post by Grégory Pakosz
On Tue, 28 Jun 2016, Grégory Pakosz wrote:

> Hello,
>
> What's the rationale behind Automake passing CFLAGS or CXXFLAGS when linking?
>
> LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
>  $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
>  $(AM_LDFLAGS) $(LDFLAGS) -o $@

Notice that these lines all start with $(LIBTOOL).  Libtool will
normally discard flags not needed for linking.  It is common for
libtool to link using the C compiler when possible and so the C
compiler can also use/discard options as needed.

Bob
--
Bob Friesenhahn
[hidden email], http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer,    http://www.GraphicsMagick.org/
Reply | Threaded
Open this post in threaded view
|

Re: Automake passing CFLAGS or CXXFLAGS when linking

Grégory Pakosz
Thanks for the reply Bob.

Well I indeed copy pasted from a Libtoolized project (by mistake) but
first I noticed CFLAGS and CXXFLAGS where used when linking in a plain
Automake project:

LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@

CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@

Also, as far as I can tell, Libtool filters flag only when linking
shared libraries.

Gregory

On Fri, Jul 1, 2016 at 3:33 PM, Bob Friesenhahn
<[hidden email]> wrote:

> On Tue, 28 Jun 2016, Grégory Pakosz wrote:
>
>> Hello,
>>
>> What's the rationale behind Automake passing CFLAGS or CXXFLAGS when
>> linking?
>>
>> LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
>>  $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
>>  $(AM_LDFLAGS) $(LDFLAGS) -o $@
>
>
> Notice that these lines all start with $(LIBTOOL).  Libtool will normally
> discard flags not needed for linking.  It is common for libtool to link
> using the C compiler when possible and so the C compiler can also
> use/discard options as needed.
>
> Bob
> --
> Bob Friesenhahn
> [hidden email], http://www.simplesystems.org/users/bfriesen/
> GraphicsMagick Maintainer,    http://www.GraphicsMagick.org/

Reply | Threaded
Open this post in threaded view
|

Re: Automake passing CFLAGS or CXXFLAGS when linking

Grégory Pakosz
For those landing on this thread, here is the commit that introduces
the behavior

commit dbf75997a9c86dcf92e94aeba6d1e5ffa1b0799c
Author: Tom Tromey <[hidden email]>
Date:   Thu May 1 01:18:21 1997 +0000

    some patches from ian
    put var index into final index
    put CFLAGS into LINK

Gregory

On Sat, Jul 2, 2016 at 9:05 AM, Grégory Pakosz <[hidden email]> wrote:

> Thanks for the reply Bob.
>
> Well I indeed copy pasted from a Libtoolized project (by mistake) but
> first I noticed CFLAGS and CXXFLAGS where used when linking in a plain
> Automake project:
>
> LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
>
> CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
>
> Also, as far as I can tell, Libtool filters flag only when linking
> shared libraries.
>
> Gregory
>
> On Fri, Jul 1, 2016 at 3:33 PM, Bob Friesenhahn
> <[hidden email]> wrote:
>> On Tue, 28 Jun 2016, Grégory Pakosz wrote:
>>
>>> Hello,
>>>
>>> What's the rationale behind Automake passing CFLAGS or CXXFLAGS when
>>> linking?
>>>
>>> LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
>>>  $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
>>>  $(AM_LDFLAGS) $(LDFLAGS) -o $@
>>
>>
>> Notice that these lines all start with $(LIBTOOL).  Libtool will normally
>> discard flags not needed for linking.  It is common for libtool to link
>> using the C compiler when possible and so the C compiler can also
>> use/discard options as needed.
>>
>> Bob
>> --
>> Bob Friesenhahn
>> [hidden email], http://www.simplesystems.org/users/bfriesen/
>> GraphicsMagick Maintainer,    http://www.GraphicsMagick.org/