[Table of contents] [Introduction to programming in Modula-3]
To create your first Modula-3 program: File Main.m3
(* Line numbers in comments, used in explanation underneath the make file: Line 1*)
MODULE Main;      (*Line 2*)
IMPORT IO;        (*Line 3*)
VAR name: TEXT;   (*Line 4*)
BEGIN                                     (*Line 5*)
  IO.Put("Enter your name: ");            (*Line 6*)
  name := IO.GetLine();                   (*Line 7*)
  IO.Put("Your name is: " & name & "\n"); (*Line 8*)
END Main.         (*Line 9*)

File m3makefile

% Makefile for Modula-3 program 'myprog' 
import("libm3")
implementation(main)
program(myprog)

File Main.m3 explanation

Notice that the elements mentioned in the Introduction to programming in Modula-3 appear in this example.

Several of the explanations underneath are very minimal, but this is due to the topics been covered in details either in later chapters of the tutorial, the reference or both.

Several features are simmilar to what you would find in C/C++ or in Pascal. To denote this such features will be followed by: (Pascal) or (C/C++)

Line 1. Comment lines go within "(* *)"(C/C++)
Line 2. This file contains the Main MODULE.
Line 3. Tells the compiler to include routines from a library called IO. In Modula-3 libraries are known as Interfaces. From here on this name will be used instead of library.
Line 4. Declaration of variable name of type TEXT. (Pascal)
Line 5. Since this program is made of of more than one statement it is necessary to have a block marked with BEGIN/END. (Pascal)
Line 6. Call to output function of interface IO.
Line 7. Populate variable name by using input function of IO interface.
Line 8. Output some literal text and the value of the variable name.
Line 9. End of module Main.

File m3makefile explanation

An SRC Modula-3 makefile (named m3makefile) contains instructions (one per line) that instruct the compiler how to build a program or library. A makefile instruction is followed by one or more arguments in parenthesis, like a procedure call. Each instruction generally specifies a library, interface, or module to be included as part of the final library or program. Comments are denoted by %. A Simple Makefile would be:

m3makefile
% Makefile for Modula-3 program 'myprog' 

import("libm3")
implementation(main)
program(myprog)

The first line is a comment. Text after % is ignored by the compiler in m3makefiles.

The import command tells the compiler that the program uses routines in the library libm3. This command is present in most m3makefiles. If you use routines from other libraries, you must include other import commands that tell the compiler which libraries to include.

There must be one implementation command for each .m3 file in your program. In this case, there was only one such file: main.m3.

Finally, the program command tells the compiler what to name the resulting executable file. The compiler will name this program's executable myprog.

  • Basic Makefile Commands: What goes in an m3makefile
  • Other Makefile Commands: More of what goes in an m3makefile


    [Table of contents] [Introduction to programming in Modula-3]