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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:24:47+00:00 2026-05-22T20:24:47+00:00

so I learned what a Makefile was some time ago, created a template Makefile

  • 0

so I learned what a Makefile was some time ago, created a template Makefile and all I do is copy and alter the same file for every program I’m doing. I changed it a few times, but it’s still a very crude Makefile. How should I improve it? This is an example of my current version:

CC  = g++
CFLAGS  = -std=gnu++0x -m64 -O3 -Wall  
IFLAGS  = -I/usr/include/igraph
LFLAGS  = -ligraph -lgsl -lgslcblas -lm 
DFLAGS  = -g -pg

# make all
all: run test                   

# make a fresh compilation from scratch 
fresh: clean test                

#makes the final executable binary
run: main.o foo1.o foo2.o              
    $(CC) $(CFLAGS) $(LFLAGS) $^ -o $@

#makes the test executable with debugging and profiling tags
test: test.o foo1.o foo2.o 
    $(CC) $(DFLAGS) $(CFLAGS) $(LFLAGS) $^ -o $@

#makes teste.o
teste.o: teste.cpp
    $(CC) $(CFLAGS) $(IFLAGS) -c $^ -o $@

#makes main.o
main.o:  main.cpp
    $(CC) $(CFLAGS) $(IFLAGS) -c $^ -o $@   

#file foo1
foo1.o: foo1.cpp
    $(CC) $(CFLAGS) $(IFLAGS) -c $^ -o $@   

#file foo2
foo2.o: foo2.cpp
    $(CC) $(CFLAGS) $(IFLAGS) -c $^ -o $@   

clean: clean-test clean-o clean-annoying

clean-test:
    rm test-rfv

clean-o:
    rm *.o -rfv

clean-annoying:
    rm *~  -rfv

Just by visually comparing with other makefiles I saw around in the web, this seems to be not a very bright Makefile. I don’t know how they work, but I can see there’s significantly less boilerplate and more generic code in them.

Can this can be made better, safer, and easier to particularize for each project?

  • 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-22T20:24:48+00:00Added an answer on May 22, 2026 at 8:24 pm

    You don’t want to name specific files in a makefile if you can get away with it, and 99% of the time you can. This page shows how to develop a very general makefile. The following is my own makefile, based on that page’s info:

    SHELL := bash
    PROG := pathed.exe
    
    OUTDIRS := bin/debug bin/rel obj/debug obj/rel
    
    PROG_REL := bin/rel/$(PROG)
    PROG_DEBUG := bin/debug/$(PROG)
    
    SRCFILES := $(wildcard src/*.cpp)
    
    OBJFILES_REL := $(patsubst src/%.cpp,obj/rel/%.o,$(SRCFILES))
    OBJFILES_DEBUG := $(patsubst src/%.cpp,obj/debug/%.o,$(SRCFILES))
    
    DEPFILES := $(patsubst src/%.cpp,obj/%.d,$(SRCFILES))
    
    CFLAGS := -Iinc -Wall -Wextra  -MMD -MP
    DBFLAGS := -g
    RELFLAGS := 
    
    CC := g++
    
    .PHONY: default all testmake debug release clean dirs
    
    default: debug 
    
    all:    dirs clean debug release
    
    dirs: 
        @mkdir -p  $(OUTDIRS)
    
    debug:  $(PROG_DEBUG)
    
    release: $(PROG_REL)
    
    testmake:
        @echo OBJFILES_REL = $(OBJFILES_REL)
        @echo OBJFILES_DEBUG = $(OBJFILES_DEBUG)
        @echo SRCFILES = $(SRCFILES)
        @echo DEPFILES = $(DEPFILES)
    
    clean:
        rm -f $(OBJFILES_REL) $(OBJFILES_DEBUG) $(DEPFILES) $(PROG)
    
    $(PROG_REL): $(OBJFILES_REL)
        $(CC)  $(OBJFILES_REL) -o $(PROG_REL)
        strip $(PROG_REL)
        @echo "----  created release binary ----"
    
    
    $(PROG_DEBUG): $(OBJFILES_DEBUG)
        $(CC) $(OBJFILES_DEBUG) -o $(PROG_DEBUG)
        @echo "----  created debug binary ----"
    
    -include $(DEPFILES)
    
    obj/rel/%.o: src/%.cpp
        $(CC) $(RELFLAGS) $(CFLAGS) -MF $(patsubst obj/rel/%.o, obj/%.d,$@) -c $< -o $@
    
    obj/debug/%.o: src/%.cpp
        $(CC) $(DBFLAGS) $(CFLAGS) -MF $(patsubst obj/debug/%.o, obj/%.d,$@) -c $< -o $@
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I learned some time ago about Decision Trees and Decision tables. I feel that
Learned all about computing algorithm costs in College, but that was so long ago
About half year ago I decided to improve my programming efficiency, so I learned
I learned that copy something to kill buffer, I can use the kill-new buffer
I learned today that NetBeans 6.5 should have an on-the-fly compilation of (single) Java
I learned Java in college, and then I was hired by a C# shop
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
I learned to use "exists" instead of "in". BAD select * from table where
I learned C++ when it was C with classes. I find myself increasingly disliking
I learned about Microsoft BizSpark the other day and started the sign up process.

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.