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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:05:51+00:00 2026-06-11T09:05:51+00:00

My makefile works alright (GNU Make), but I was wondering whether There is a

  • 0

My makefile works alright (GNU Make), but I was wondering whether

  • There is a way to make a new directory “bin” and put all the .o in there
  • If the answer to “1” is “yes”, can the instruction “make clean” delete the folder “bin”?
  • Avoid having to write all the .cpp and .o filenames?
CFLAGS = -Wall -g
CC = g++
EXEC = main
OBJ = listpath.o Parser.o main.o

all: $(EXEC)

listpath.o: src/listpath.cpp
  $(CC) $(CFLAGS) -c src/listpath.cpp

Parser.o: src/Parser.cpp 
  $(CC) $(CFLAGS) -c src/Parser.cpp

main.o: src/main.cpp
  $(CC) $(CFLAGS) -c src/main.cpp

$(EXEC): $(OBJ)
  $(CC) $(CFLAGS) $(OBJ) -o $(EXEC)

.cpp.o:
  $(CFLAGS) $<

.PHONY: clean
clean:
  rm $(OBJ) $(EXEC)
  • 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-11T09:05:52+00:00Added an answer on June 11, 2026 at 9:05 am

    Starting with the last question, you can define implicit rules to convert .c files to .o files.

    bin/%.o : src/%.cpp | bin
      $(CC) $(CFLAGS) -c $< -o $@
    

    This will also out the .o files to the bin/ directory.

    Now your result rule will look like:

    $(EXEC): $(addprefix bin/, $(OBJ))
      $(CC) $(CFLAGS) $^ -o $@
    

    To clean the bin/ directory, simple change your clean rule to:

    clean:
      rm -rf bin/ $(EXEC)
    

    Finally, add a rule to create the bin directory:

    bin:
      mkdir -p bin
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I use just make to compile, my Makefile works fine. But when I
I want to make a bash script that works like a makefile. It would
I have a gnu makefile template that has served me well, but when I
I'm reading about how to put a makefile together, but no-one seems to mention
Alright so this is my current setup for a makefile. There are files which
I would like some advice about the following Makefile. It works fine, but it
I'm trying to read an Makefile in C/C++ it works by doing: system(make -C
It is a long time since I haven't done Makefiles. My actual Makefile works
I have a makefile structured something like this: all : compile executable clean :
I'm updating a Makefile that accesses some resources from an external source, i.e. there

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.