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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:17:11+00:00 2026-05-19T14:17:11+00:00

i was wondering if anyone have sometime to answer some questions about GNU makefiles…

  • 0

i was wondering if anyone have sometime to answer some questions about GNU makefiles…

  1. how to create a directory if it doesn’t exists (“./obj”) for output?
  2. i have one makefile, but i got 2 build methods “Debug” and “Release”, can i have both in 1 makefile and how to tell it which one to build?
  3. ive been using Code::Blocks which builds only changed files, but my makefile builds them everytime i call make command, without touching any files. how can i make it build changed files only?

here is my current makefile

OBJPATH=./obj
COMPILER=gcc

Output: main.o Base64.o
 $(COMPILER) -o Output.exe $(OBJPATH)/main.o $(OBJPATH)/Base64.o
 strip Output.exe

main.o: main.c main.h
 $(COMPILER) -c main.c -o $(OBJPATH)/main.o

Base64.o: Base64.c Base64.h
 $(COMPILER) -c Base64.c -o $(OBJPATH)/Base64.o

thanks.

  • 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-19T14:17:12+00:00Added an answer on May 19, 2026 at 2:17 pm

    For the first question, you can put a fake target before any of the others, along the lines of:

    preamble:
        -mkdir obj
    
    main.o: preamble main.c
        blah blah blah
    

    That will automatically execute everything in the preamble (you have to make it the first dependency in every rule) before it builds anything else. The - at the start of the mkdir ignores failures if, for example, the directory already exists.

    For the second question, you can provide something like:

    all: debug release
    
    debug: blah blah blah
    
    release: blah blah blah
    

    and actually put the debug and release code in separate subdirectories. That way, you can build either with make release or make debug and build them both with make all.

    Third question: Your makefile builds every time because the rules tell it to. For example, Output: main.o Base64.o will always try to build since Output never exists (the correct target seems to be Output.exe).

    Similarly your object file rules will always execute since neither main.o nor Base64.o are updated by their statements (they update the files in the obj directory instead).

    You may be able to fix that case by making the target $(OBJPATH)/main.o but, to be honest, I don’t usually worry about separating objects and executables into separate directories. I tend to just lump them all into one directory and let make -clean clean them up.


    So the makefile I would start with would be:

    COMPILER=gcc
    
    # Meta rules
    
    all: release debug
    
    release: Output.exe
    
    debug: Output-d.exe
    
    # Release stuff
    
    Output.exe: main.o Base64.o
        $(COMPILER) -o Output.exe main.o Base64.o
        strip Output.exe
    
    main.o: main.c main.h
        $(COMPILER) -c main.c -o main.o
    
    Base64.o: Base64.c Base64.h
        $(COMPILER) -c Base64.c -o Base64.o
    
    # Debug stuff
    
    Output-d.exe: main-d.o Base64-d.o
        $(COMPILER) -g -o Output-d.exe main-d.o Base64-d.o
    
    main-d.o: main.c main.h
        $(COMPILER) -g -DDEBUG -c main.c -o main-d.o
    
    Base64-d.o: Base64.c Base64.h
        $(COMPILER) -g -DDEBUG -c Base64.c -o Base64-d.o
    

    In response to your comment question:

    Is there anyway I can re-set a variable based on the Target Selected? for example if selected release OBJPATH will be “./obj/Release” if selected debug OBJPATH = “./obj/Debug”?

    GNU Make may be more powerful than the ones I’m used to but you can do that by setting an environment variable then re-running make as per the following:

    all: release debug
    
    release:
            ( export zzvar=release ; $(MAKE) zz_Output.exe )
    
    debug:
            ( export zzvar=debug ; $(MAKE) zz_Output-d.exe )
    
    zz_Output.exe:
            echo $(zzvar)
            touch zz_Output.exe
    
    zz_Output-d.exe: zz_main-d.o zz_Base64-d.o
            echo $(zzvar)
            touch zz_Output-d.exe
    

    which outputs:

    ( export zzvar=release ; make zz_Output.exe )
    make[1]: Entering directory '/home/pax'
    echo release
    release                                             <==
    touch zz_Output.exe
    make[1]: Leaving directory '/home/pax'
    
    ( export zzvar=debug ; make zz_Output-d.exe )
    make[1]: Entering directory '/home/pax'
    echo debug
    debug                                               <==
    touch zz_Output-d.exe
    make[1]: Leaving directory '/home/pax'
    

    You can see the two separate variables marked with <== above.

    As I said, there’s probably an easier way to do it with GNU Make but that’ll get you started.

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

Sidebar

Related Questions

I am just wondering if anyone have some info or feedback about the situation
I'm struggling getting some unit tests running and wondering if anyone might have anything
I was wondering if anyone knew why jQuery doesn't have a simple $().id() method.
I was wondering if anyone could help me with this. I have to create
I guess I have two questions really. 1) I was wondering if anyone knows
all, I have some problem installing my apk file in code. Wondering if anyone
I have a text file, I was wondering anyone have a batch file to
I'm wondering if anyone have a good resource for working with Corba in Python?
I like jQuery and I was wondering if anyone have used a good plugin
I was wondering if anyone would help with the following: I have a date

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.