title: Makefile: nothing to be done
body: <p>I'm building a parser using Bison and Flex. To compile everything I use a simple makefile. However, I'm not an expert in creating makefile. The makefile is working, except for a few things:</p>
  
  <p>Changing source code in file.cpp isn't detected by the makefile. 
  <strong>Nothing to be done.</strong>
  A <code>make clean</code> fixes the problem, but this should not be necessary of course. I have found a lot of makefile tutorials, but not any discusses this kind of problems.</p>
  
  <pre><code>CC=g++
  CFLAGS=-Wall -std=c++11 -ll
  EXECUTABLE=parser 
  OBJS=parser.tab.cpp lex.yy.c 
  
  FILES= file.cpp
  
  all: parser
  
  parser.tab.cpp parser.tab.hpp: parser.ypp
      bison -d parser.ypp
  
  lex.yy.c: parser.l parser.tab.hpp
      flex parser.l
  
  parser: $(OBJS) $(FILES)
      $(CC) $(CFLAGS) $(OBJS) $(FILES) -o $(EXECUTABLE) 
  
  clean:
      rm -rf *o $(EXECUTABLE) $(OBJS)
  </code></pre>
  
  <p>Of course, I could use <code>make -B all</code>, and then I have detected something strange:</p>
  
  <pre><code>bison -d parser.ypp
  bison -d parser.ypp
  flex parser.l
  g++ -Wall -std=c++11 -ll parser.tab.cpp lex.yy.c file.cpp -o parser
  </code></pre>
  
  <p>Why is <code>bison -d parser.ypp</code> done twice?</p>
  
  <p><strong>[SOLVED]</strong></p>
  
  <p>We use now the following makefile:</p>
  
  <pre><code>CC = g++
  CFLAGS = --std=c++0x -O2
  
  SOURCES = $(wildcard */*.cpp *.cpp) parser/parser.cpp
  OBJECTS = $(SOURCES:.cpp=.o) scanner/scanner.o parser/parser.o
  
  EXECUTABLE = calculator
  
  all: parser/parser.cpp scanner/scanner.cpp $(EXECUTABLE)
  
  parser/parser.cpp:
      cd parser &amp;&amp; bison -d -o parser.cpp grammar.ypp &amp;&amp; cd ..
  
  scanner/scanner.cpp: parser/parser.hpp
      cd scanner &amp;&amp; flex -o scanner.cpp lexer.l &amp;&amp; cd ..
  
  $(EXECUTABLE): $(OBJECTS)
      $(CC) $^ -o "$@"
  
  %.o: %.cpp
      $(CC) $(CFLAGS) -c "$&lt;" -o "$@"
  
  clean:
      rm -rf $(OBJECTS) $(EXECUTABLE) parser/parser.cpp parser/parser.hpp scanner/scanner.cpp
  </code></pre>