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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:24:55+00:00 2026-05-30T05:24:55+00:00

How do I use different compiler flags for different source files in a Makefile?

  • 0

How do I use different compiler flags for different source files in a Makefile? For example, I’d like a Makefile that will produce:

g++ -c -COMPILER_FLAGS_1   -g source1.cpp -o source1.o

g++ -c -COMPILER_FLAGS_2   -g source2.cpp -o source2.o
g++ -c -COMPILER_FLAGS_2   -g source3.cpp -o source3.o
g++ -c -COMPILER_FLAGS_2   -g source4.cpp -o source4.o

g++ -c -COMPILER_FLAGS_3   -g source5.cpp -o source5.o
g++ -c -COMPILER_FLAGS_3   -g source6.cpp -o source6.o
g++ -c -COMPILER_FLAGS_3   -g source7.cpp -o source7.o

g++ -g -o output source1.o source2.o source3.o source4.o source5.o source6.o source7.o 

At the moment I’ve got about 20 source files (and that’s expected to grow), so an easy to maintain file would be nice.

Thanks in advance.

  • 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-30T05:24:57+00:00Added an answer on May 30, 2026 at 5:24 am

    You could do something like the following (untested, so the syntax might be slightly off):

    OBJS_1 := source1.o
    OBJS_2 := source2.o source3.o source4.o
    OBJS_3 := source5.o source6.o source7.o
    
    OBJS := $(OBJS_1) $(OBJS_2) $(OBJS_3)
    
    output: $(OBJS)
        $(CXX) -g -o $@ $^
    
    $(OBJS_1): CXXFLAGS := $(COMPILER_FLAGS_1)
    $(OBJS_2): CXXFLAGS := $(COMPILER_FLAGS_2)
    $(OBJS_3): CXXFLAGS := $(COMPILER_FLAGS_3)
    
    $(OBJS): %.o: %.cpp
        $(CXX) -c $(CXXFLAGS) -g $< -o $@
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute a php-script from php that will use different constants and
I am trying to use different open source apps in my project. Problem is
I would like to use different instances of an STL custom allocator class to
is there a chance to use different location for apache's config files (on Windows)?
I'd like to use a different icon for the demo version of my game,
I have a c++ project with various extensions for the source files (.cpp, .c,
Background There are several different debug flags you can use with the Visual Studio
I am writing my first project that will use autoconf and teaching it to
I'm building a Flex application that will need run under two different deployment scenarios:
I found that different compiler optimization levels in gcc give quite different results when

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.