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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:24:36+00:00 2026-05-11T09:24:36+00:00

I have the following makefile that I use to build a program (a kernel,

  • 0

I have the following makefile that I use to build a program (a kernel, actually) that I’m working on. Its from scratch and I’m learning about the process, so its not perfect, but I think its powerful enough at this point for my level of experience writing makefiles.

AS  =   nasm CC  =   gcc LD  =   ld  TARGET      =   core BUILD       =   build SOURCES     =   source INCLUDE     =   include ASM         =   assembly  VPATH = $(SOURCES)  CFLAGS  =   -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions \             -nostdinc -fno-builtin -I $(INCLUDE) ASFLAGS =   -f elf  #CFILES     =   core.c consoleio.c system.c CFILES      =   $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) SFILES      =   assembly/start.asm  SOBJS   =   $(SFILES:.asm=.o) COBJS   =   $(CFILES:.c=.o) OBJS    =   $(SOBJS) $(COBJS)  build : $(TARGET).img  $(TARGET).img : $(TARGET).elf     c:/python26/python.exe concat.py stage1 stage2 pad.bin core.elf floppy.img  $(TARGET).elf : $(OBJS)     $(LD) -T link.ld -o $@ $^  $(SOBJS) : $(SFILES)     $(AS) $(ASFLAGS) $< -o $@  %.o: %.c     @echo Compiling $<...     $(CC) $(CFLAGS) -c -o $@ $<  #Clean Script - Should clear out all .o files everywhere and all that. clean:     -del *.img     -del *.o     -del assembly\*.o     -del core.elf 

My main issue with this makefile is that when I modify a header file that one or more C files include, the C files aren’t rebuilt. I can fix this quite easily by having all of my header files be dependencies for all of my C files, but that would effectively cause a complete rebuild of the project any time I changed/added a header file, which would not be very graceful.

What I want is for only the C files that include the header file I change to be rebuilt, and for the entire project to be linked again. I can do the linking by causing all header files to be dependencies of the target, but I cannot figure out how to make the C files be invalidated when their included header files are newer.

I’ve heard that GCC has some commands to make this possible (so the makefile can somehow figure out which files need to be rebuilt) but I can’t for the life of me find an actual implementation example to look at. Can someone post a solution that will enable this behavior in a makefile?

EDIT: I should clarify, I’m familiar with the concept of putting the individual targets in and having each target.o require the header files. That requires me to be editing the makefile every time I include a header file somewhere, which is a bit of a pain. I’m looking for a solution that can derive the header file dependencies on its own, which I’m fairly certain I’ve seen in other projects.

  • 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. 2026-05-11T09:24:37+00:00Added an answer on May 11, 2026 at 9:24 am

    As already pointed out elsewhere on this site, see this page: Auto-Dependency Generation

    In short, gcc can automatically create .d dependency files for you, which are mini makefile fragments containing the dependencies of the .c file you compiled. Every time you change the .c file and compile it, the .d file will be updated.

    Besides adding the -M flag to gcc, you’ll need to include the .d files in the makefile (like Chris wrote above). There are some more complicated issues in the page which are solved using sed, but you can ignore them and do a ‘make clean’ to clear away the .d files whenever make complains about not being able to build a header file that no longer exists.

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

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "how quickly you can code" so totally concerns me that… May 11, 2026 at 10:02 pm
  • Editorial Team
    Editorial Team added an answer I use SQL Server 2005 in combination with Visual Studio… May 11, 2026 at 10:02 pm
  • Editorial Team
    Editorial Team added an answer scope to the end of the line: Y is REAL… May 11, 2026 at 10:02 pm

Related Questions

I'm a C Newb I write lots of code in dynamic languages (javascript, python,
I've gotten fed up with MSVC++6 and how everyone is always telling me that
I have installed Eclipse and CDT (to use C/C++ in eclipse CDT is needed),
I have no problem compiling specific code the following way: g++ -I /opt/local/include Code1.cc

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.