3
votes

I'm trying to create a Makefile that will compile and run 3 different implementations of a markov algorithm all at once. I'm new to makefiles so if there's multiple mistakes, please let me know. Also, if I wanted to optimize the compiles with -O3, where would I do that?

When I run, I currently get these errors:

Makefile:28: warning: overriding commands for target `markov.o'

Makefile:22: warning: ignoring old commands for target `markov.o'

make: * No rule to make target Markov.java', needed byjava_markov.class'. Stop.

Here is the code for my makefile:

javaC=javac
javaR=java
CC=g++
CC=gcc

CFLAGS=-O0
OPT=-deprecation
TARGET1=./java_markov
TARGET2=./markov_cpp
TARGET3=./markov_c
INFILE=./alice30.txt
OUTFILE1=./markov_java_out.txt
OUTFILE2=./output/markov_cpp_out.txt
OUTFILE3=./output/markov_c_out.txt

$(TARGET1).class: Markov.java
    $(javaC) Markov.java

$(TARGET2): markov.o
    $(CC) $(CFLAGS) -o $(TARGET2) markov.o
markov.o: markov.cpp
    $(CC) $(CFLAGS) -c markov.cpp


$(TARGET3) : markov.o eprintf.o
    $(CC) $(CFLAGS) -o $(TARGET3) markov.o eprintf.o
markov.o : markov.c
    $(CC) $(CFLAGS) -c markov.c
eprintf.o : eprintf.c eprintf.h
    $(CC) $(CFLAGS) -c eprintf.c

clean:
    rm -f *.class $(OUTFILE1)
    rm -f *.o $(TARGET2) $(OUTFILE2)
    rm -f *.o $(TARGET3) $(OUTFILE3)
run: $(TARGET1).class
    $(javaR) $(TARGET1) < $(INFILE) > $(OUTFILE1)
    $(TARGET2)
    $(TARGET2) <$(INFILE) >$(OUTFILE2)
    $(TARGET3)
    $(TARGET3) < $(INFILE) > $(OUTFILE3)
1
4th line, you set CC to gcc (GNU compiler collection) after setting it to g++. On my computer using gcc on a .cpp file doesn't work as it ends up running the C compiler. Try using one variable for gcc on .c sources, and another for g++ on .cpp sourcesPete
Why does this have to be a make file? Do a make file for the C and C++ versions, then do your running in a shell or python script.Falmarri

1 Answers

3
votes
  • You do have two rules to make markov.o. Try renaming one of them, if you really have C and C++ ports in the same folder: e.g.

    markov-c++.o: markov.cpp
    $(TARGET2): markov-c++.o …
    
  • You really don't have to use 2-step compilation with an intermediate .o file, if you're not linking in any more .o's. You could just do

    $(TARGET2): markov.cpp
        $(CC) $(CFLAGS) markov.cpp -o $(TARGET2)
    
  • The other warning indicates that there's no file named Markov.java in the directory. Is it perhaps in a subdirectory or something?

  • make run is pretty broken :-) I think you just wanted to put all three targets on the prerequisites line?