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

  • Home
  • SEARCH
  • 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 6084989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:35:16+00:00 2026-05-23T11:35:16+00:00

Your small C/C++ project has reached a point where it’s no longer practical to

  • 0

Your small C/C++ project has reached a point where it’s no longer practical to have all your code in one file. You want to split out a few components. So you make a src/ directory, and then… you have to write a real Makefile. Something more than hello: hello.o. Uh-oh… was it $@ or $< or $^? Crap. You don’t remember (I never do).

Do you have a ‘one-size fits all’ simple Makefile that can deal with straightforward source trees? If so, what’s in it and why? I’m looking for the smallest, simplest Makefile that can compile a directory full of C files nicely without me having to edit the Makefile every time I add a file. Here’s what I have so far:

CXX = clang++
CXXFLAGS = ...
LDFLAGS = ...
EXENAME = main

SRCS = $(wildcard src/*.cc)
OBJS = $(patsubst src%.cc,build%.o, $(SRCS))

all: $(EXENAME)

build/%.o: src/%.cc
    @mkdir -p $(dir $@)
    $(CXX) -c -o $@ $^ $(CXXFLAGS)

$(EXENAME): $(OBJS)
    $(CXX) -o $@ $^ $(LDFLAGS) 

clean:
    rm -rf $(EXENAME) build/

This Makefile builds all the .cc files in the src/ directory into .o files in the build/ directory, then links them up into the parent directory.

What would you do differently?

  • 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-23T11:35:17+00:00Added an answer on May 23, 2026 at 11:35 am

    I would reconsider you decision not to have an explicit list of sources– I think it may cause you trouble in the long run. But if that’s your decision, this makefile is pretty good.

    In the %.o rule I would use $< instead of $^, so that later you can add dependencies like

    build/foo.o: bar.h
    

    And when you’re ready, you can take a look at Advanced Auto-Dependency Generation.

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

Sidebar

Related Questions

i have a small java project i want to build using a makefile, the
I am building a small Recipe Organiser MVC project with EntityFramework Code first and
I have a project where I must make the following; You have a small
I am working on a small project where I use multiple classes. One of
I am working on a small project and need your help. Here are the
Your job is to design a Project Plan class library which supports the tracking
I am currently designing and implementing a small programming language as an extra-credit project
I'm learning Python and would like to start a small project. It seems that
I just finished a small project written in C, where I read a data
What's the best way to make small schema updates to your symfony/doctrine application? My

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.