0

I am attempting to use Makefile for the first time. Is it possible to compile 2 different c++ programs into 2 executables using one Makefile?

Here is what I've done so far but it will only compile the first one.

CFLAGS=-O3 -Wall

series : ./problem1/series.cpp
    g++ $(CFLAGS) -o series ./problem1/series.cpp

gn : ./problem1/gn.cpp
    g++ $(CFLAGS) -o gn ./problem1/gn.cpp

EDIT:

How can I stop it from compiling every time (unless I modify the code)?

CFLAGS=-O3 -Wall

all : q1_series q1_gn q1_series_new_initial

q1_series : ./problem1/series.cpp
    g++ $(CFLAGS) -o ./executables/q1_series ./problem1/series.cpp

q1_gn : ./problem1/gn.cpp
    g++ $(CFLAGS) -o ./executables/q1_gn ./problem1/gn.cpp

q1_series_new_initial : ./problem1/series1.cpp
    g++ $(CFLAGS) -o ./executables/q1_series_new_initial ./problem1/series1.cpp

    echo "Question 1 created in ./executables"
1

2 Answers 2

1

Yes it is. You make a "Fake" target, that is dependent on both other programs

all_programs : series gn 
0
1

By default when you run make make runs the rules for the first target it sees. In your case that would be the series target.

You can manually run any other target(s) you want by specifying them on the command line make gn or make gn series for example.

If you want make to default to building more than the first target you give it a first target that does what you want.

The general name for this target is all. So you would add all: series gn before the current target entries.

You will likely also want to mark that target as .PHONY: all so that in case some all file ever managed to be created running make all would still work the way you expect (and not attempt to check the modification time of the all file against its prerequisites, etc.).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.