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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:57:46+00:00 2026-06-13T00:57:46+00:00

I am trying to write a Makefile for a small C program. It’s structure

  • 0

I am trying to write a Makefile for a small C program. It’s structure is

project_root

  • src (source files)
  • tests (test files)
  • build (program binary)

My make file is below.

CFLAGS = -g -Wall -Isrc $(OPTFLAGS)
LDLIBS = $(OPTLIBS)

PREFIX ?= /usr/local/
OPTVALGRIND="valgrind --track-origins=yes --log-file=tests/valgrind.log"

TARGET_SRC = src/hello_make.c
TARGET = build/hello_make

SOURCES := $(wildcard src/*.c)
SOURCES := $(filter-out $(TARGET_SRC),$(SOURCES))

OBJECTS = $(patsubst %.c,%.o,$(SOURCES))

TEST_SRC = $(wildcard tests/*_tests.c)
TESTS = $(patsubst %.c,%,$(TEST_SRC))
TEST_FILE ?= hello_make_tests

all: $(TARGET) tests

$(TARGET): build $(OBJECTS)
    $(CC) $(CFLAGS) $(TARGET_SRC) $(OBJECTS) $(LDLIBS) -o $@ 

.PHONY: compile_tests tests test testv valgrind build clean
compile_tests: LDLIBS += -lrt -lm
compile_tests: CFLAGS += $(OBJECTS)
compile_tests: build $(OBJECTS) $(TESTS)
    @echo "TARGET=$(TARGET), TESTS=$(TESTS)"
    @echo "compile_tests done"

tests: compile_tests
    sh ./tests/run_tests.sh

test: compile_tests
    sh ./tests/run_test.sh tests/$(TEST_FILE)

testv: compile_tests
    sh ./tests/run_test.sh tests/$(TEST_FILE)

valgrind:
    VALGRIND=$(OPTVALGRIND) $(MAKE) -s

clean:
    rm -rf $(OBJECTS)
    rm -rf $(TARGET)
    rm -rf $(TESTS)
    rm -rf build
    rm -rf tests/*.log

build:
    @mkdir -p build

I have a target test and testv(corresponding for running with valgrind).
So when I do,

make test TEST_FILE=hello_make_tests

it should build anything needed for this test. Then run tests/hello_make_tests.

I have most of the Makefile working. It builds and compiles the source files to .o
links those to the test program.
$\

My problem is that this only works if I use make all first then followed by make test.
When I do make clean followed by make test. I get the following error.

cc -g -Wall -Isrc  src/get_one.o src/get_three.o src/get_two.o   -c -o src/get_one.o src/get_one.c
cc: error: src/get_one.o: No such file or directory
cc: error: src/get_three.o: No such file or directory
cc: error: src/get_two.o: No such file or directory
make: *** [src/get_one.o] Error 1

The cc call seems to be wrong with object files acting as sources? I am pretty new to make and am
unable to figure this out. Please help.

  • 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-06-13T00:57:48+00:00Added an answer on June 13, 2026 at 12:57 am

    This line

    compile_tests: CFLAGS += $(OBJECTS)
    

    adds all objects to the CFLAGS variable which will be used by the implicit rules (that are built into make) when compiling.

    Try to keep source, compiler flags and resulting object files separate in your makefile to avoid this kind of confusion.

    Have a quick look at the documentation for the makefile flags if you’re uncertain of their usage.

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

Sidebar

Related Questions

I am trying to write a makefile for compiling a program with source files
I'm trying to write a makefile snippet to compile .el files to .elc files.
I am trying to write a makefile for a small scale application I wrote
Trying to write a code at the moment that basically tests to see if
I am trying to write a relatively simple Makefile, but I am not sure
I'm trying to write a Makefile where prerequisites using target specific variables version= target1:
I'm trying to write a Makefile for a python script, that will create a
I am trying to write a Makefile for a JVM port. I have already
I need to write a pre-build makefile which is called separately from the main
I'm trying to write a makefile to package up some PHP scripts. I would

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.