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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:57:00+00:00 2026-05-27T09:57:00+00:00

I have a makefile that can be reduced to this: OBJS = obj1.o obj2.o

  • 0

I have a makefile that can be reduced to this:

OBJS = obj1.o obj2.o

FLAGS = -Wall -Wextra -Werror -pedantic-errors -fno-rtti -std=c++0x
DEBUG_FLAGS = -ggdb -O0 -fstack-protector-all -D_GLIBCXX_DEBUG
RELEASE_FLAGS = -O3

release: $(OBJS)
    g++ $(FLAGS) $(RELEASE_FLAGS) $(OBJS)

debug: $(OBJS)
    g++ $(FLAGS) $(DEBUG_FLAGS) $(OBJS)

obj1.o: obj1.cpp
    g++ -c $(FLAGS) obj1.cpp

obj2.o: obj2.cpp
    g++ -c $(FLAGS) obj2.cpp

The problem is that all or none of the files must be built with the -D_GLIBCXX_DEBUG flag. I don’t know how to do this without writing two entries for every compilation unit, like

obj1_release.o: obj1.cpp
    g++ -c $(FLAGS) $(RELEASE_FLAGS) obj1.cpp

obj1_debug.o: obj1.cpp
    g++ -c $(FLAGS) $(DEBUG_FLAGS) obj1.cpp

How can I make the -D_GLIBCXX_DEBUG flag (and the other debug flags) take effect for all compilation units only when the user types make debug without writing two entries for every CU? (And vice versa; the release flags need to take effect on all CUs when the user types make release.)

I apologise if this is the basics of writing Makefiles, I don’t know much about them.

  • 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-27T09:57:01+00:00Added an answer on May 27, 2026 at 9:57 am

    You are looking for pattern rules: something like this should do what you want. Note that this cannot be made to work correctly unless the debug and release versions of the program are given different names.

    OBJS   := obj1 obj2 obj3
    R_OBJS := $(OBJS:=_r.o)
    D_OBJS := $(OBJS:=_d.o)
    
    all: prog_r prog_d
    release: prog_r
    debug: prog_d
    
    prog_r: $(R_OBJS)
        $(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) $(LDFLAGS) $^ $(LIBS) -o $@
    
    prog_d: $(D_OBJS)
        $(CXX) $(CXXFLAGS) $(DEBUG_FLAGS) $(LDFLAGS) $^ $(LIBS) -o $@
    
    %_r.o: %.cc
        $(CXX) $(CXXFLAGS) $(RELEASE_FLAGS) -c $< -o $@
    
    %_d.o: %.cc
        $(CXX) $(CXXFLAGS) $(DEBUG_FLAGS) -c $< -o $@
    
    # header files
    obj1_d.o obj1_r.o: foo.h bar.h
    obj2_d.o obj2_r.o: quux.h
    # ... etc ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Makefile that looks like this CXX = g++ -O2 -Wall all:
I have a section of makefile that has this sort of structure: bob: ifdef
I have a project that can be built via makefile, and I would like
I have a makefile which runs commands that can take a while. I'd like
Is there a way that I can set my kernel Makefile flags to always
I have a GNU Makefile that looks a bit like this: LIST = item1
Hopefully this is a very simple question. I have a makefile pattern rule that
I have a working makefile that builds with mingw32. Now i renamed that makefile
I have a makefile project that builds and links a DLL, using the command-line
I have a project that has a makefile with broken dependencies. Is there any

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.