Quine in Gnu Make

Published on 02 August 2023 (Updated: 07 August 2023)

Welcome to the Quine in Gnu Make page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

q=$(info q=$(value q))$(info $$(q))$(eval q:;@:)
$(q)

Quine in Gnu Make was written by:

This article was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

I can't take credit for this solution. I got the code from Rosetta Code.

Let's break it down. First, there is this variable assignment:

q=$(info q=$(value q))$(info $$(q))$(eval q:;@:)

Since = is used, deferred evaluation is done. This means that the expression is not evaluated until it is needs to be. In other words, the variable q is just set to the value after the equal sign.

The real magic doesn't happen until this line:

$(q)

This forces q to be evaluted. The first expression to be evaluated is this:

$(info q=$(value q))

The value function returns the value of the specified variable without expanding it. The info function displays the text that follows it. Putting those together, this displays the q= followed by the value of q. This displays the first line of the code.

The second expression to be evaluated is this:

$(info $$(q))

The $$ is just the way that a literal $ character must be represented since $ has a special meaning in GNU Make. Therefore, this just displays the second line of the code.

The final expression to be evaluated is this:

$(eval q:;@:)

The eval function evaluation the expression that follows it and returns an empty string. In this case, it is creating this target:

q:;@:

Since GNU Make is a build system, it needs something to build, or else it will give this error:

make: *** No targets.  Stop.

To give make something to do, this "do nothing" target called q is provided. The q target is written in the alternate form.

target:;command

This means that whenever the target needs to be built, make will execute the command following the semicolon. By default, make echoes each command that it executes. To suppress this, @ may be used before the command. The colon (:) command just exits with non-error status.

How to Run the Solution

To run this program, download and install the latest GNU Make using these instructions:

Download a copy of Quine in GNU Make, and run this command:

make -sf quine.mk