DEV Community

Fernando Paladini
Fernando Paladini

Posted on

Scaffold and Edit Flat Makefiles from the Terminal with MakeMaker and C++11

Writing a small C or C++ utility often starts with two or three source files and a few compiler commands. A full CMake or Meson setup can feel heavy for a throwaway prototype or lab exercise, yet editing a raw makefile by hand frequently introduces space-instead-of-tab errors or inconsistent flags across targets.

MakeMaker (mm) is an MIT-licensed C++11 command-line tool that creates, inspects, and updates flat makefile definitions from the terminal. Instead of hand-formatting tab-indented recipe blocks, you run mm set -v, mm add, mm edit, mm list, and mm remove to maintain variables and compiler targets.

This tutorial shows how to compile mm locally with g++ without root privileges, scaffold a multi-file C++ build, verify the generated makefile with GNU Make, and stay within the grammar and safety boundaries of MakeMaker's parser.

TL;DR

Compile src/Run.cpp into a local mm binary with g++ -std=c++11, define top-level variables with mm set -v, add compiler recipes with mm add, and run make in the same directory:

mkdir -p binary
g++ src/Run.cpp -o binary/mm -std=c++11

mkdir demo && cd demo
../binary/mm set -v CXX=g++ CXXFLAGS=-std=c++11
../binary/mm add build '$(CXX)' main.cpp greeter.cpp '$(CXXFLAGS)' -Wall -o greeter_app
../binary/mm list -A
make build
Enter fullscreen mode Exit fullscreen mode

Use MakeMaker only for flat makefile layouts (variables at the top, followed by target: headers and tab-indented commands). Do not run mm in directories whose existing makefile relies on # comments, target prerequisites, .PHONY, or conditionals, because every valid mm command re-serializes ./makefile.

Prerequisites

You need:

  • A C++11-compatible g++ compiler (verified with GCC 13.2.0).
  • GNU Make (make or gmake) to run the generated makefile.
  • curl and unzip (or git) to fetch the repository.

Release and packaging facts:

  • MakeMaker uses the MIT License (LICENSE).
  • The repository (paladini/MakeMaker), built for a 2015 Software Engineering course at UFSC, publishes no tagged GitHub releases. All behavior below reflects the master branch at commit cb3c4a0.

1. Download and compile MakeMaker without sudo

The project's makefile defines an install target that runs sudo cp binary/mm /usr/bin/mm. You do not need root access to build or run mm. Every class lives in headers included by src/Run.cpp, so one g++ command produces the executable:

curl -fsSL https://github.com/paladini/MakeMaker/archive/refs/heads/master.zip -o makemaker.zip
unzip -q makemaker.zip
cd MakeMaker-master

mkdir -p binary
g++ src/Run.cpp -o binary/mm -std=c++11
./binary/mm
Enter fullscreen mode Exit fullscreen mode

On GCC 13, compilation exits 0 with one -Wreturn-type warning in src/FileManager.hpp where the private unused singleton operator= has an empty {} body. Avoid -Werror=return-type unless you change that declaration to = delete;. Running ./binary/mm with no arguments prints the CLI usage guide.

2. Scaffold a multi-file C++ build

MakeMaker reads and writes a lowercase makefile in the current working directory. Create a clean demo folder and two small C++ source files to verify the generated build end to end:

mkdir demo && cd demo

cat << 'EOF' > greeter.hpp
#ifndef GREETER_HPP
#define GREETER_HPP
#include <string>
std::string greet(const std::string& name);
#endif
EOF

cat << 'EOF' > greeter.cpp
#include "greeter.hpp"
std::string greet(const std::string& name) {
    return "Hello from " + name + "!";
}
EOF

cat << 'EOF' > main.cpp
#include <iostream>
#include "greeter.hpp"
int main() {
    std::cout << greet("MakeMaker") << std::endl;
    return 0;
}
EOF
Enter fullscreen mode Exit fullscreen mode

Define Makefile variables, remove a temporary variable, and add build and check targets:

../binary/mm set -v CXX=g++ CXXFLAGS=-std=c++11 TEMP_VAR=unused
../binary/mm set -v TEMP_VAR=
../binary/mm add build '$(CXX)' main.cpp greeter.cpp '$(CXXFLAGS)' -Wall -o greeter_app
../binary/mm add check ./greeter_app
Enter fullscreen mode Exit fullscreen mode

Single quotes around '$(CXX)' and '$(CXXFLAGS)' prevent your shell from expanding $ early, writing literal Make variable references into makefile.

3. Update, rename, and prune targets

MakeMaker addresses individual recipe lines with 1-based <target>:<line> syntax. Create a draft target, rename it to release, attach and refine its command, and delete a temporary target:

../binary/mm add draft_target
../binary/mm edit draft_target release
../binary/mm add release '$(CXX)' main.cpp greeter.cpp -O2 -o greeter_release
../binary/mm edit release:1 '$(CXX)' main.cpp greeter.cpp '$(CXXFLAGS)' -O2 -Wall -o greeter_release
../binary/mm add obsolete ./greeter_app
../binary/mm remove obsolete
Enter fullscreen mode Exit fullscreen mode

4. Verify the makefile and run GNU Make

Inspect targets, variables, line-numbered commands, and the full makefile:

../binary/mm list
../binary/mm list -V
../binary/mm list release
../binary/mm list -A
Enter fullscreen mode Exit fullscreen mode

Expected output from mm list and mm list -A:

MakeMaker has found 3 targets.
=================================
build
check
release
=================================
CXX=g++
CXXFLAGS=-std=c++11

build:
    $(CXX) main.cpp greeter.cpp $(CXXFLAGS) -Wall -o greeter_app

check:
    ./greeter_app

release:
    $(CXX) main.cpp greeter.cpp $(CXXFLAGS) -O2 -Wall -o greeter_release
Enter fullscreen mode Exit fullscreen mode

Execute GNU Make and run the compiled binary:

make build
./greeter_app
Enter fullscreen mode Exit fullscreen mode

Expected result:

g++ main.cpp greeter.cpp -std=c++11 -Wall -o greeter_app
Hello from MakeMaker!
Enter fullscreen mode Exit fullscreen mode

Why It Works

MakeMaker splits parsing and serialization across five small headers in src/:

  • FileManager (src/FileManager.hpp): Singleton wrapper around "makefile" in the working directory; creates an empty "makefile" if none exists.
  • FileInterpreter (src/FileInterpreter.hpp): Parses top-level KEY=VALUE lines into Variable objects until the first non-tabbed line containing :. Each target: header starts a Target, and subsequent lines become Command entries on that target.
  • Command (src/Command.hpp): Splits a recipe on whitespace. Token 0 becomes Compiler; tokens not starting with - become File items (main.cpp, greeter.cpp, $(CXXFLAGS)); tokens starting with - become Flag items (pairing a flag like -o with a following non-- token like greeter_app). to_command() always writes \t<compiler> <files...> <flags...>.
  • CommandInterpreter and MakeFile (src/CommandInterpreter.hpp, src/MakeFile.hpp): Dispatch CLI verbs (add, set, remove, edit, list), update the in-memory vectors, and write _mk.to_string() back to "makefile". Passing KEY= throws VariableNullValue in src/Variable.hpp, which parse_set() catches to delete the variable.

Failure Modes, Limitations, and Security Boundaries

Keep these source-verified constraints in mind:

  • Every subcommand rewrites ./makefile: CommandInterpreter::parse() calls _mk.save() even after mm list, and FileManager creates an empty makefile if missing. Never run mm inside a folder with a handwritten makefile you want left untouched.
  • No # comments, prerequisites, or conditionals: FileInterpreter models only top-level KEY=VALUE lines, target: headers, and recipe lines. A # comment before the first target triggers std::out_of_range on temp->at(-1), and target prerequisites after : (such as build: main.o) are stripped when re-saved.
  • mm edit <target>:<line> requires a trailing -flag: parse_edit() calls CommandInterpreter::parse_files(), which loops while (input[0] != '-') across argv without checking argc. Always include at least one --prefixed flag (such as -Wall or -o app) when editing a command line.
  • Confirm target names before mm list <target>: MakeFile::list_target() dereferences get_target_as_pointer(targetName) without a NULL check. Run mm list first to check exact target names.
  • Privilege and shell boundaries: Compile and invoke ./binary/mm locally rather than running sudo make install. MakeMaker writes raw recipe strings without shell escaping, so never pass untrusted input into mm arguments and always inspect makefile before running make.

FAQ

Does MakeMaker replace CMake or GNU Make?

No. MakeMaker generates and edits a basic makefile; GNU Make (make) still executes the build recipes.

Why did mm ignore my uppercase Makefile?

CommandInterpreter hardcodes lowercase "makefile". On case-sensitive filesystems such as Linux ext4, Makefile and makefile are separate files, and GNU Make reads makefile before Makefile if both exist.

Can one target hold multiple commands?

Yes. Run mm add <target> <command> repeatedly for the same target. Use mm list <target> to inspect 1-based line numbers for mm edit <target>:<line> or mm remove <target>:<line>.

How do I delete a variable?

Run mm set -v VAR_NAME= with an empty value.

Takeaway

MakeMaker turns repetitive flat makefile edits into quick terminal commands with zero external runtime dependencies. Compile binary/mm locally with g++ -std=c++11, keep variables at the top and targets flat, and verify every generated recipe with mm list -A and make.

For small C or C++ prototypes, do you still start with a plain makefile, or do you reach straight for CMake, Just, or a task runner?

AI assistance disclosure: This tutorial was researched, smoke-tested locally with GCC 13.2.0 and GNU Make, and drafted with AI assistance. Commands, parser behavior, limitations, and safety boundaries were verified against the public paladini/MakeMaker source (cb3c4a0) and live CLI runs.

Top comments (0)