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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:41:39+00:00 2026-06-06T18:41:39+00:00

I am new to make files and I put this together with a bit

  • 0

I am new to make files and I put this together with a bit of trial & error. This code is used to compile my c++ program.

My main.cpp file in the same folder as the makefile. I have a lib/ folder that contains the headers main depends on.

The following makefile results in a correct and complete compilation of my code. But I was expecting that I would find *.o objects left behind. (Note that I’ve tried to make both with and without the “clean” rule, and I get the same results both times.)

#
# Makefile
#

CXX = g++
CCFLAGS = -O3 -I/sw/include -L/sw/lib
      ## /sw/include and /sw/lib contain dependencies for files in my lib/
LDFLAGS = -lpng
OPTS = $(CCFLAGS) $(LDFLAGS)

SOURCES = $(wildcard lib/*.cpp) main.cpp
OBJECTS = $(SOURCES: .cpp = .o)
TARGET = spirals

$(TARGET): $(OBJECTS)
    $(CXX) $(OPTS) $^ -o $@

.PHONY: depend
depend:
    g++ -MM $(SOURCES) > depend
      ## generate dependencies list
include depend

.PHONY: clean
clean:
    rm -f *.o lib/*.o $(TARGET)

Also, in case it matters, I’m on MacOSX and my program is designed in xcode. (I know that xcode has its own build flow, but I’m designing a command-line program for a linux system and I’d like to test compilation & linking in a bash environment instead of only going through xcode.)

Questions:

  1. Am I correct to expect makefiles to produce *.o files that stick around once the main target has been created?

  2. If so, why doesn’t my makefile do this?

  • 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-06T18:41:40+00:00Added an answer on June 6, 2026 at 6:41 pm

    If you observe what command your $(TARGET) rule causes to be run:

    g++ -O3 -I/sw/include -L/sw/lib -lpng lib/bar.cpp lib/foo.cpp main.cpp -o spirals
    

    you’ll see that $(OBJECTS) in fact contains *.cpp files, and there are no *.o files sticking around because you haven’t asked for any.

    The problem is here:

    OBJECTS = $(SOURCES:.cpp=.o)
    

    In your GNU makefile as written, this substitution reference is written with excess spaces, so never matches anything and $(OBJECTS) ends up the same as $(SOURCES). Rewrite it as above and it’ll do what you expect.

    (Other notes: -lpng needs to go at the end of the link command to work in general, so you should introduce another make variable (traditionally called $(LDLIBS)) so as to arrange that. Especially as someone new to makefiles, you would do better to spell out your dependencies explicitly rather than playing games with $(wildcard) and a computed $(OBJECTS). -I options are needed during compilation while -L options are used during linking, so it would be good to arrange separate $(CXXFLAGS)/$(LDFLAGS) variables used in separate rules so they are only added when required.)

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

Sidebar

Related Questions

I'm new to C programming. I want to compile C program using Make file.
New to batch files, first try actually. Trying to make a simple batch file
Having some issues with this search engine I am trying to put together. I'll
I am trying to save some files using C, with this code: sprintf(playerinput,%s,end); sprintf(fileloc,%s/.notend,getenv(HOME));
I am trying to make a new file format for music. It needs to
I was wondering if it is possible to make a new file inside the
I have created a file with XmlDocument xmldoc = new XmlDocument(); Can I make
I created a new HalloWorld Makefile Project. There is a HalloWorld.cpp with my main
I want to make new stackpanal from another stackpanal already made before but if
So all I simply want to do is make a Ruby program that reads

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.