Some Simple Examples

The simplest way to run cm3 is to create a new directory, put some .m3 and .i3 files in it and then just run cm3. Cm3 will automatically scan for source files and detect their relationships, build your program and leave it in TARGET/prog where TARGET is the platform you are using (for example, NT386 or SPARC.)

If you haven't yet, you may consider reviewing the CM3-IDE User Guide or the Modula-3 Tutorial.

The Simplest Example

In Main.m3, you put:

And then run "cm3" in the current directory (assuming there are no other Modula-3 files in that directory.) This should generate a derived directory for object files (named after your platform, like NT386, LINUXELF, or SPARC) and inside it include an executable program named "prog" which you can run. It's that simple!

Yet Another Simple Example

In fact, you can put together multiple modules together this way also. Create a directory hello and put all these files in it:

In Main.m3:

In A.i3:

In A.m3:

Now run cm3 in that directory. It automatically recognizes your program dependencies and will generate an executable program for you, without a need for makefiles, arguments, etc. Cm3 will figure out everything from your programs.

Of course, you could have told cm3 which files to include by specifying them at the command line.

Using cm3 with an m3makefile

Beyond simple programs, you will probably want to create an m3makefile. For doing so, follow the conventions below. CM3-IDE has options for creating new packages, so you don't have to do this work by hand if you don't want to. But CM3-IDE does not do any magic--anything it does you should be able to do yourself from the command line.

For example, here's a simple program composed of a main module, an imported interface and its implementation.

To begin, create a fresh directory for the package and within that directory, a directory for the source files:

    > mkdir hello
    > cd hello
    > mkdir src

Create the following source files in the src directory:

In the file src/Main.m3:

    MODULE Main;
      A.DoIt ();
    END Main.

In the file src/A.i3:

    PROCEDURE DoIt ();
    END A.

In the file src/A.m3:

    IMPORT Wr, Stdio;

    PROCEDURE DoIt () =
      <*FATAL ANY*>
        Wr.PutText (Stdio.stdout, "Hello world.\n");
        Wr.Close (Stdio.stdout);
      END DoIt;

    END A.

In the file src/m3makefile:

    import ("libm3")
    implementation ("Main")
    module ("A")
    program ("foo")

Finally, from the package directory, hello, run cm3. This should will compile the three source files and link them with the standard libraries. The derived files will be placed in a directory that names the architecture. On an Alpha/AXP machine running OSF, the directory is called ALPHA_OSF. The executable program will be named foo in the derived directory.