Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

649
Views
Makefile Fatal error: can't create obj/calc.o

I'm trying to make a makefile for a simple calculator for a college project. I need it done, and I've searched the web for tutorials and I eventually found this code:

IDIR =include
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=obj
LDIR=lib
SDIR=src

LIBS=-lm

_DEPS = calc.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = calc.o libcalc.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

calc: $(OBJ)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

The output of the make command is

gcc -c -o obj/calc.o calc.c -I../include
Assembler messages:
Fatal error: can't create obj/calc.o: No such file or directory
make: *** [obj/calc.o] Error 1

My professor gave us no lead nor instruction to this, so I'm really lost.

My project is divided in three folders: lib, src and include. src contains the source for the project, named calc.c, include contains the header named calc.h, and lib contains the library named libcalc.c.

EDIT: I figured out this error, and it was that the obj directory under src wasn't created. However, it's still not working. The new error message is:

make: *** No rule to make target `obj/libcalc.o', needed by `calc'.  Stop.

EDIT 2: I just moved the libcalc.c from lib to obj and it worked fine. Not ideal, but it does what I need.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The obj directory doesn't exist, so gcc cannot write the output file there.

You could create it automatically from the Makefile, but depending on what you want to do, a simple mkdir obj might be all you need.

However, it looks like you are running your Makefile from the src directory. Chances are you want to run it from your project's root directory and specify src/ where needed. You'd want to add an SDIR variable (or similar) for that; make sure to remove the ../ on LDIR, IDIR and ODIR in that case.

For example, where you currently use %.c, use $(SDIR)/%.c instead, and move your Makefile appropriately.

over 4 years ago · Santiago Trujillo Report

0

according to your cli command, you are running your makefile under src/, which is fine, but gcc won't create missing folder for you. here is the code runs in my linux:

$ tree
.
└── test2.c

0 directories, 1 file

$ gcc -c -o obj/test2.o test2.c
Assembler messages:
Fatal error: can't create obj/test2.o: No such file or directory

$ mkdir obj

$ gcc -c -o obj/test2.o test2.c

$ tree
.
├── obj
│   └── test2.o
└── test2.c

1 directory, 2 files
over 4 years ago · Santiago Trujillo Report

0

Do touch main.o in src/obj that worked for me

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!