I'm learning flex and (for really the first time) using the command line.

I created a Makefile to simplify my testing process but I don't understand why the commands are in reverse.

For instance, in the terminal the order is as such:

flex test.l

this creates the file lex.yy.c

then I compile that:

g++ lex.yy.c -ll -o test

The Makefile reads backwards:

test: lex.yy.c
     g++ lex.yy.c -ll -o test

lex.yy.c: test.l
     flex test.l

So what happens specifically when I run make?

2 Answers 2


Makefiles follow this format (Makefiles should always use tabs instead of spaces, since it is required in most (if not all) implementations of make) :

target: dependencies
    operations to build target
  • The target is what you're willing to build/compile/create. There may be several of them, and they should be built in the Makefile order, unless dependencies need to be met first. The first target in your file is called the default target, it is what make tries to build when you call it without arguments.
  • The dependencies are the different pieces required to build a target.

In this Makefile, you have two targets:

  • test
  • lex.yy.c

Since lex.yy.c is a dependency required to build test, it'll be built first from test.l. Once it is generated, it'll be possible to compile test. Basically, make... :

  • Tries to build test.
  • Unresolved dependency. lex.yy.c does not exist (or has been updated), it needs to be built first.
  • Reading operations to build lex.yy.c. Running flex test.l : lex.yy.c is built.
  • All dependencies for test are met. Running g++ lex.yy.c -ll -o test.
  • test is created.

Additional info: For more information about makefiles, I would recommend The Linux Development Platform by Rafeeq Ur Rehman. Chapter 4: Using GNU Make.

  • So does it have to be in reverse order, can I build lex.yy.c first then build test? Thank you, btw!
    – MayNotBe
    Commented Aug 28, 2014 at 18:35
  • You should add that the first target is the default target. It is made if one calls make without arguments. Apart from that the order of targets does not matter so much. That is actually the reason for the "reverse order" of the targets in the makefile of the OP.
    – Tobias
    Commented Aug 28, 2014 at 18:36
  • 3
    @MayNotBe it doesn't matter what order the targets appear in the makefile, it only matters that you have the target and its dependencies proper.
    – casey
    Commented Aug 28, 2014 at 18:37
  • 1
    You should note that at least for "tradional" makefiles, and maybe all makefiles, the lines for "operations to build target" have to start with an ASCII tab character. This can cause problems, as spaces and tabs can render identically on screnn.
    – user732
    Commented Aug 28, 2014 at 18:37
  • @Tobias: Edited my answer, thank you. Bruce: Added information about spaces and tabs in Makefiles. I do believe that this is relevant for all makefiles, at least I've always seen it this way. Commented Aug 28, 2014 at 18:39

Make calls the target test first, but lex.yy.c is a dependency.

So before perform the real execution, make analyzes your targets and all dependencies. The first target is test, but before start to execute it, lex.yy.c is called.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .