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 9290125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:20:36+00:00 2026-06-18T20:20:36+00:00

I need a Makefile that will compile everything in the current directory and recursively

  • 0

I need a Makefile that will compile everything in the current directory and recursively down the tree, and preferably use the compiler’s dependency thing (-M, etc.) so that whenever I type “make”, as little as possible is recompiled.

Also, why is this not page 1 of the Makefile documentation?

  • 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-06-18T20:20:38+00:00Added an answer on June 18, 2026 at 8:20 pm

    While I would suggest using tools like cmake or alike, I understand that sometimes it is easier or better to use a plain old Makefile.

    Here’s a Makefile I’ve used on some projects, it creates dependency-files using gcc:

    # Project Name (executable)
    PROJECT = demoproject
    # Compiler
    CC = g++
    
    # Run Options       
    COMMANDLINE_OPTIONS = /dev/ttyS0
    
    # Compiler options during compilation
    COMPILE_OPTIONS = -ansi -pedantic -Wall
    
    #Header include directories
    HEADERS =
    #Libraries for linking
    LIBS =
    
    # Dependency options
    DEPENDENCY_OPTIONS = -MM
    
    #-- Do not edit below this line --
    
    # Subdirs to search for additional source files
    SUBDIRS := $(shell ls -F | grep "\/" )
    DIRS := ./ $(SUBDIRS)
    SOURCE_FILES := $(foreach d, $(DIRS), $(wildcard $(d)*.cpp) )
    
    # Create an object file of every cpp file
    OBJECTS = $(patsubst %.cpp, %.o, $(SOURCE_FILES))
    
    # Dependencies
    DEPENDENCIES = $(patsubst %.cpp, %.d, $(SOURCE_FILES))
    
    # Create .d files
    %.d: %.cpp
        $(CC) $(DEPENDENCY_OPTIONS) $< -MT "$*.o $*.d" -MF $*.d
    
    # Make $(PROJECT) the default target
    all: $(DEPENDENCIES) $(PROJECT)
    
    $(PROJECT): $(OBJECTS)
        $(CC) -o $(PROJECT) $(OBJECTS) $(LIBS)
    
    # Include dependencies (if there are any)
    ifneq "$(strip $(DEPENDENCIES))" ""
      include $(DEPENDENCIES)
    endif
    
    # Compile every cpp file to an object
    %.o: %.cpp
        $(CC) -c $(COMPILE_OPTIONS) -o $@ $< $(HEADERS)
    
    # Build & Run Project
    run: $(PROJECT)
        ./$(PROJECT) $(COMMANDLINE_OPTIONS)
    
    # Clean & Debug
    .PHONY: makefile-debug
    makefile-debug:
    
    .PHONY: clean
    clean:
        rm -f $(PROJECT) $(OBJECTS)
    
    .PHONY: depclean
    depclean:
        rm -f $(DEPENDENCIES)
    
    clean-all: clean depclean
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a makefile that will compile my simpleprogram.c to sp and
I need to modify the below makefile to create a dll (SampleNew.dll) that will
I want to use a exe that will create a triangle strip. I have
we have some C++ code that we need to create a make file in.
I need a little nmake makefile for my build process. The file types are
I need to create a special makefile rule, which is best explained by an
I need to put the xcodeprojfile in the same dir as the Makefile for
I have a makefile for compiling Arduino programs . I need to add some
I have couple of cpp and hpp files in directory ./src . I compile
I'm trying to create a make file which will compile all the .cpp 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.