.cpp to .o files in Makefile -


how can generate .o file corresponding .cpp files in directory using makefile? have directory contains .cpp files. now, want compile them in .o files. name of .o files should same corresponding .cpp files. should do?

actually, had implementation not sure how work

srcs := $(wildcard $(srcdir)/*.cpp) objs := $(srcs:$(srcdir)/%.cpp=$(objdir)/%.o)  $(objs): $(objdir)/%.o : $(srcdir)/%.cpp             // recipe // 

try this

srcs=$(wildcard *.cpp) objs=$(srcs:.cpp=.o )  %.o: %.cpp     $(cc) -c $< -o $@ 

let me add more explaination it.

srcs := $(wildcard $(srcdir)/*.cpp) - list .cpp files under srcdir directory , assign srcs

objs := $(srcs:$(srcdir)/%.cpp=$(objdir)/%.o) - replace files .cpp listed above statement .o ex: main.cpp changed main.o , assigned objs

$(objdir)/%.o : $(srcdir)/%.cpp - object files depends on respective .cpp files , in rule can write rule create object files

the % character can used wildcard pattern-matching, provide generic targets. example:

%.o: %.c [tab] actions 

when % appears in dependency list, replaced same string used perform substitution in target.

if above explanation not clear go through makefile basics once , try writing without using special variables , go complex rules. check this tutorial


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -