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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:36:07+00:00 2026-05-15T21:36:07+00:00

I have a small project that builds a number of targets from the same

  • 0

I have a small project that builds a number of targets from the same source files. The targets require building the source files with different compile flags. This is actually on cygwin so I’ll use that as the concrete example although I suppose this is a generic problem.

So this would be an example Makefile:

a: CFLAGS =

a: a.o c.o

b: CFLAGS = -mno-cygwin

b: b.o c.o

This works in principle, building a will compile with CFLAGS unset, and building b will compile with CFLAGS set to -mno-cygwin. But only if c.o does not already exist.

So doing

> make a b

in a virgin directory will first compile a.c and c.c using an empty CFLAGS. Then it will try to build b using CFLAGS=-mno-cygwin. But since c.o already exists it will not recompile c.c resulting in linker errors since object files need to have the same setting of this flag.

Again, the cygwin-flag is just one concrete example, and I am looking for a generic solution.

What I have tried is to introduce an extra target checking the current CFLAGS and remove all object files if it does not match:

ARCH    = `echo $(CFLAGS)`
checkTarget:
-@if test "`cat .arch`" != "$(ARCH)"; then \
    rm *.o; \
    /bin/echo -n $(ARCH) > .arch; \
fi

Inserting this target as a dependency for the two targets ensures that all object files are recompiled if required.

a: CFLAGS =

a: checkTarget a.o c.o

b: CFLAGS = -mno-cygwin

b: checkTarget b.o c.o

This however recompiles every object file, which is unnecessary, and becomes a problem in a larger project.

Is there a better way to do this?

EDIT:
In the comments to the single answer included a hint that you could make the object files depend on the content of the CFLAGS. I can’t figure out how to do that, except cat’ing them to a temporary file and compare to the previous ones and then copy that over a depending file. Is there no better way?

  • 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-15T21:36:07+00:00Added an answer on May 15, 2026 at 9:36 pm

    Although there where some suggestions on how to do what I initially asked for here (as pointed to by slowdog), I took the decision to take Beta‘s different-names-schema one step further and put all objects for the various variants in subdirectories following the description in Beta‘s second suggestion in his answer to this question.

    In essence that made my Makefile look like:

    A : AOBJDIR = .a
    AOBJECTS = $(addprefix $(AOBJDIR)/,$(ASRCS:.c=.o))
    
    $(AOBJECTS): $(AOBJDIR)/%o: %.c
        $(CC) $(CFLAGS) -MMD -o $@ -c $<
    
    $(AOBJDIR) :
        @mkdir $(AOBJDIR)
    
    -include $(AOBJECTS:.o=.d)
    
    a: $(AOBJDIR) $(AOBJECTS)
        $(LINK) ...
    

    So the first part names a subdirectory to be used for object files of ‘a’ and creates a list of object files in that directory by converting the source filenames and adding the subdirectory prefix.

    Then follows the default rule for those object files (with dependency generation thrown in for good measure).

    Next two are ensuring that the subdirectory exists, and that the dependency information is included.

    Finally the link rule with an extra dependency on the subdirectory itself to ensure that it is created if it does not exist.

    This block can be repeated for b, in my example, by exchanging all a’s. So if I only could figure out how to package this block into something more general that can be parameterized I would be happy.

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

Sidebar

Related Questions

I have a small project that I was using node-dirty for, but it's not
I have a small project that I will be working on shortly that collects
I have a small project and I noticed that I have a lot of
I have a small- to medium-size project that I am doing for my software
I have a small RIA that I built as a learning/make-my-life-easier project that uses
I have a small Visual studio add-in that walks down active project and insert
I have a relatively small project (50 classes) that has included 13 JAR libraries
I have a Java project that builds using Apache Ant to a website. This
I have a .NET project (VS2008 .NET 3.5) that builds an exe. I have
I have a small maven project that build a java jar file. I added

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.