File Input Output in Gnu Make (original) (raw)
Published on 13 July 2023 (Updated: 29 July 2023)
Welcome to the File Input Output 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
define TEXT
Hello from GNU Make
Here are some lines:
This is line 1
This is line 2
Goodbye!
endef
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mo>></mo><mi>o</mi><mi>u</mi><mi>t</mi><mi>p</mi><mi>u</mi><mi>t</mi><mi mathvariant="normal">.</mi><mi>t</mi><mi>x</mi><mi>t</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">(file >output.txt,</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8095em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal">u</span><span class="mord mathnormal">tp</span><span class="mord mathnormal">u</span><span class="mord mathnormal">t</span><span class="mord">.</span><span class="mord mathnormal">t</span><span class="mord mathnormal">x</span><span class="mord mathnormal">t</span><span class="mpunct">,</span></span></span></span>(TEXT)) <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>i</mi><mi>n</mi><mi>f</mi><mi>o</mi></mrow><annotation encoding="application/x-tex">(info </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">o</span></span></span></span>(file <output.txt))
.PHONY: all
all: ;@:
File Input Output in Gnu Make was written by:
- rzuckerm
This article was written by:
- Ron Zuckerman
- rzuckerm
If you see anything you'd like to change or update, please consider contributing.
How to Implement the Solution
The define keyword assigns a multi-line value to a variable. The value is terminated with an endef
keyword. For this sample program, this is the text that will be written to the output file:
define TEXT
Hello from GNU Make
Here are some lines:
This is line 1
This is line 2
Goodbye!
endef
The file function allows files to be read and written. The general form of this function is this:
$(file op filename[,text])
where:
op
is the file operation:<
means input from the specified file.>
means output to the specified file.>>
means append to the specified file.
filename
is the path to the file.text
is the text to be written to the file for the>
and>>
file operations.
This writes the text (stored in the TEXT
variable) to a file calledoutput.txt
:
This reads the text from output.txt
and displays it using the infofunction.
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, a "do nothing" target called all
is provided like this:
The .PHONY keyword specifies targets that will always be built, whether or not they exist. In this case, that is the all
target. This target is written in the alternate form for brevity:
This means that whenever the target needs to be built, make
will execute the command following the semicolon. By default, make
echoes each commandthat it executes. To suppress this, @
may be used before the command. Thecolon (:) 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 File Input Output in GNU Make, and run this command:
make -sf file-input-output.mk