Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:38:32+00:00 2026-05-15T12:38:32+00:00

Lets say I have files: Libs: one.cpp, one.h two.cpp, two.h three.cpp, three.h Program: program.cpp

  • 0

Lets say I have files:

Libs:

  • one.cpp, one.h
  • two.cpp, two.h
  • three.cpp, three.h

Program:

  • program.cpp

Is there way, to create Makefile which will compile only that *.cpp which were modified from last compilation?

Currently I have something like that:

SRCS = one.cpp two.cpp three.cpp
OBJS = $(SRCS:.cpp=.o)

all: $(OBJS) program

.cpp.o:
    g++ -Wall -c $<

program:
    g++ -Wall $(OBJS) program.cpp -o program

clean:
    rm -f $(OBJS) program

I works fine, but when I compile my program and then change two.cpp or two.h I need to run “make clean” first, because when I secondly run “make” I get:

Nothing to be done for 'all'.

I would like to change my Makefile in that way, it would recognize my changes and recompile that file and its dependencies (if one.cpp uses code from two.cpp which was modified, both files should be recompiled).

So if I modify two.cpp, make should do:

g++ -Wall -c two.cpp
g++ -Wall $(OBJS) program.cpp -o program

But if one.cpp uses code from two.cpp which was modified, make shold do:

g++ -Wall -c one.cpp
g++ -Wall -c two.cpp
g++ -Wall $(OBJS) program.cpp -o program
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:38:33+00:00Added an answer on May 15, 2026 at 12:38 pm

    First we make the object files prerequisites of the executable. Once this is done, Make will rebuild program whenever one of the SRCS changes, so we don’t need OBJS as an explicit target:

    all: program
    
    program: $(OBJS)
      g++ -Wall $(OBJS) program.cpp -o program
    

    Then we make the header files prerequisites of the objects, so that if we change three.h, Make will rebuild three.o:

    $(OBJS): %.o : %.h
    

    And finally since one.cpp uses code from two.cpp by means of two.h (I hope), we make two.h a prerequisite of one.o:

    one.o: two.h
    

    And to make things cleaner and easier to maintain we use automatic variables:

    program: $(OBJS)
      g++ -Wall $^ program.cpp -o $@
    

    Put it all together and we get:

    SRCS = one.cpp two.cpp three.cpp
    OBJS = $(SRCS:.cpp=.o)
    
    all: program
    
    $(OBJS): %.o : %.h
    
    one.o: two.h
    
    .cpp.o:
      g++ -Wall -c $<
    
    program: $(OBJS)
      g++ -Wall $^ program.cpp -o $@
    
    clean:
      rm -f $(OBJS) program
    

    There are a few more things we could do (like adding program.o to OBJS), but this is enough for today.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files lets say a.xls and b.xls . The first one contains
Lets say i have at least two lua script files. test1.lua test2.lua both define
Lets say I have 3 files: add.h, add.cpp and main.cpp. This is a common
Lets say I have a two websites www.sample.com and files.sample.com. In an .htaccess file
Lets say I have two databases: one for students and one for classes. I
Let's say I have two files default.aspx and the associated default.aspx.cs . default.aspx.cs: protected
In c lets say we have 2 files 1.h #include<2.h> blah blah and we
Lets say I have some pdf files stored on my server and I only
Lets say I have a C++ program and I want to call functions from
Lets say I have a multi-threaded application that needs to resize some image files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.