help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Additional environment information with make 4


From: Christian Hujer
Subject: Re: Additional environment information with make 4
Date: Fri, 6 Mar 2015 18:54:38 +0530

Hi Markus,

there are a few things you can improve about your little Makefile.

Short:
* avoid '@' - instead use -s
* Use the built-in rules, they're good
* Follow the conventions of the built-in rules to make them work
* Declare non-file goals .PHONY
* Tell make about dependencies to make it run incrementally
* Add a clean rule to remote the stuff that make was building

Long:
* I recommend to avoid '@' and use it only in rare cases.
Sooner or later you will have a Makefile which doesn't run as
expected, either by itself or because one of the commands that it
calls is hanging.
Then you will be thankful for the command echo.
You will not want to have to change the Makefile and remove the '@' on
all the recipes just in order to see what's going on.

Instead, use the `-s` command line option.
With '-s' you can tell make that you're not interested in seeing
what's going on.
And if that's the usual case, you can define an environment variable like this:
export MAKEFLAGS=-s

See also https://www.gnu.org/software/make/manual/make.html#Echoing

* Use the built-in rules, they're good.
An overview of the built-in rules can be seen here:
https://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules
So, make already knows how to compile C++ programs, you don't need to tell it.
Also, I find it useful to keep what to build (rules which specify
dependencies) and how (pattern-rules which specify recipes) separate
from each other.

* Follow conventions of the built-in rules to make them work
The built-in rules expect that one of the object files has the same
basename as the binary that you are trying to build.
So, instead of building example from main.cpp, you would build example
from example.cpp.
This is a convention which is also there outside the world of make.
It's quite common practice to name the source file that contains main
after the intended program name.

The following Makefile is sufficient to tell make to compile
example.cpp into example:

all: example

No further line is needed, make knows the rest by itself.

* Declare non-file goals .PHONY
It's recommended to declare goals which do not exist as a file as .PHONY.
This prevents the Makefile from falling over in case the user creates
a file or directory with the same name as one of these goals.

So, above all rule should actually look like this:

.PHONY: all
all: example

* Tell make about dependencies to make it run incrementally.
For example, if you run `make run`, it would fail unless you ran `make` first.
Why not tell make that if you want to `make run` and example doesn't
exist, that example needs to be made first?

Change
run:
        ./example
to
.PHONY: run
run: example
        ./example

Now make knows that if you want to run, it first has to build.
It would also rebuild in case example.cpp is newer than example.

* Add a clean rule to remove the stuff that make was building.

So here's your final Makefile:
.PHONY: all
all: example

.PHONY: run
run: example
        ./example

.PHONY: clean
clean:
        $(RM) example

I hope this helps you in your future ventures with make.

On Thu, Feb 26, 2015 at 6:33 PM, Markus Fischer <address@hidden> wrote:
> On 26.02.2015 13:20, David wrote:
>> Explained here:
>> https://www.gnu.org/software/make/manual/html_node/Echoing.html#Echoing
>>
>> Try changing that line in your makefile to:
>> <tab>@./example
>>
>
> Thanks, this got me on the right path.
>
>
> _______________________________________________
> Help-make mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-make



reply via email to

[Prev in Thread] Current Thread [Next in Thread]