Eagle
Eagle

Reputation: 3474

creating a makefile for multiple targets

i have created a Makefile which i would change so, that it will generate more then one target when i run make.

In my program i use self predefined macros (e.g. TIME, REG and _DEBUG ) and i would like to have my Makefile i that way that it will generate one target with out predefined macros, one with REG and another one with REG and TIME.

I hope that what i wish is a reasonable wish from Makefile, if not, then please let me know.

P.S.:

recommendations would be gladly excepted

I am using Here is my Makefile:

CXX = g++
SOURCES = random.cpp
OBJECTS = $(SOURCES:.cpp=.o) 
EXECUTABLE = random-64bit
DEBUG = -g -p -ggdb
CXXFLAGS = -Wall -ansi -pedantic -W -pipe -O3 -std=gnu++0x -march=native \
    --fast-math -ftree-vectorize -ffast-math -D NDEBUG \
    -D TIME -D REG -D _DEBUG
#CXXFLAGS+=$(DEBUG)
DEPS = def_type.hpp \
    ls_regression.hpp \
    network.hpp \
    statistics.hpp \
    knot.hpp \
    nachbarn.hpp \
    $(SOURCES:.cpp=.hpp) \
    zufallszahlengenerator.hpp 
INCLUDES = -I/home/phymihsa/eigen/ -I/home/phymihsa/boost_1_48_0
LIBPATH = -L/usr/local/lib64
LDFLAGS = -lm

all: $(SOURCES) $(EXECUTABLE) 

$(EXECUTABLE): $(OBJECTS) $(DEPS) 
    $(CXX) $(LIBPATH) $(OBJECTS) -o $@

.cpp.o: $<
    $(CXX) $(LIBPATH) $(INCLUDES) $(LDFLAGS) $(CXXFLAGS) -c $<

%.o: %.cpp
    $(CXX) $(LIBPATH) $(INCLUDES) $(LDFLAGS) $(CXXFLAGS) -c $<


.PHONY: clean

clean:
    rm -rf $(OBJECTS) $(EXECUTABLE) *~ p1 *.o 

EDIT

Based on the answer of @trojanfoe, i would to know if it is possible to use arrays

OBJECTS = $(SOURCES:.cpp=_none.o) $(SOURCES:.cpp=_reg.o) $(SOURCES:.cpp=_reg_time.o)
EXECUTABLE = $(SOURCES:.cpp=_none) $(SOURCES:.cpp=_reg) $(SOURCES:.cpp=_reg_time)

instead of each one writing each one specific?

Upvotes: 3

Views: 5393

Answers (3)

Eagle
Eagle

Reputation: 3474

Here is what i was searching for:

CXX = g++
OPTIONS := none reg reg_inter reg_time
    none_CXXFLAGS  :=
     reg_CXXFLAGS  := -D REG
reg_inter_CXXFLAGS := $(reg_CXXFLAGS) -D INTERMEDIATE_STEP
 reg_time_CXXFLAGS := $(reg_CXXFLAGS) -D TIME
EXECUTABLES = $(addprefix random_,$(OPTIONS))
DEBUG = -g3 -p -ggdb
CXXFLAGS = -Wall -ansi -pedantic -W -pipe -O3 -std=gnu++0x -march=core2 -mtune=core2 \
           --fast-math -ftree-vectorize -ffast-math -D NDEBUG
CXXFLAGS+=$(DEBUG)
DEPS = def_type.hpp \
       ls_regression.hpp \
       network.hpp \
       statistics.hpp \
       knot.hpp \
       nachbarn.hpp \
       zufallszahlengenerator.hpp
INCFLAGS = -I/usr/include/eigen3 -I/usr/include/boost_1_48
LIBPATH = -L/usr/lib64
LDFLAGS = -lm

.PHONY: all
all: $(EXECUTABLES)

random_%: random_%.o
    $(CXX) $(LDFLAGS) $(LIBPATH) $^ -o $@

random_%.o : random.cpp $(DEPS)
    $(CXX) $(INCFLAGS) $(CXXFLAGS) $($*_CXXFLAGS) -c $< -o $@

.PHONY: clean
clean:
    rm -rf $(OBJECTS) $(EXECUTABLES) *~ p1 *.o

in this solution there is a use of variables instead of writing each time the all text

regards

Upvotes: 2

user1157391
user1157391

Reputation:

For each file, create one target per compile flag variant.

Say you have a program that depends on two files, foo.cc and bar.cc. You want to compile a debug and a release version. You'll do something like this:

DEBUG = -g -Wall -D DEBUG
RELEASE = ...

foo_debug.o: foo.cc
    g++ -o foo_debug.o -c foo.cc $(DEBUG)
foo_release.o: foo.cc
    g++ -o foo_release.o -c foo.cc $(RELEASE)

# Same thing for bar ...

main_debug: foo_debug.o bar_debug.o
    g++ -o main_debug foo_debug.o bar_debug.o

main_release: foo_release.o bar_release.o
    g++ -o main_release foo_release.o bar_release.o

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122381

You'll need to generate 3 different versions of the object file from random.cpp and 3 different executables. Note $(CXXFLAGS) contains flags common across all 3 targets and I've also moved $(DEPS) to be a dependency of the object files, not the executable.

CXX = g++
SOURCES = random.cpp
OBJECTS = $(SOURCES:.cpp=.o) 
EXECUTABLE = random-64bit
DEBUG = -g -p -ggdb
CXXFLAGS = -Wall -ansi -pedantic -W -pipe -O3 -std=gnu++0x -march=native \
    --fast-math -ftree-vectorize -ffast-math -D NDEBUG -D _DEBUG
#CXXFLAGS+=$(DEBUG)
DEPS = def_type.hpp \
    ls_regression.hpp \
    network.hpp \
    statistics.hpp \
    knot.hpp \
    nachbarn.hpp \
    $(SOURCES:.cpp=.hpp) \
    zufallszahlengenerator.hpp 
INCLUDES = -I/home/phymihsa/eigen/ -I/home/phymihsa/boost_1_48_0
LIBPATH = -L/usr/local/lib64
LDFLAGS = -lm

all: random_none random_reg random_reg_time

random_none: random_none.o
    $(CXX) $(LDFLAGS) $(LIBPATH) random_none.o -o $@

random_reg: random_reg.o
    $(CXX) $(LDFLAGS) $(LIBPATH) random_reg.o -o $@

random_reg_time: random_reg_time.o
    $(CXX) $(LDFLAGS) $(LIBPATH) random_reg_time.o -o $@

random_none.o: random.cpp $(DEPS)
    $(CXX) $(INCLUDES) $(CXXFLAGS) -c $<

random_reg.o: random.cpp $(DEPS)
    $(CXX) $(INCLUDES) $(CXXFLAGS) -D REG -c $<

random_reg_time.o: random.cpp $(DEPS)
    $(CXX) $(INCLUDES) $(CXXFLAGS) -D REG -D TIME -c $<

.PHONY: clean

clean:
    rm -rf *.o random_none random_reg random_reg_time *~ p1

Upvotes: 0

Related Questions