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

  • Home
  • SEARCH
  • 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 6978601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:46:40+00:00 2026-05-27T17:46:40+00:00

I’ve tried numerous attempts to move my .o files to my obj folder, but

  • 0

I’ve tried numerous attempts to move my .o files to my obj folder, but no matter what I do, it simply just doesn’t work.

Judging from the makefile provided, what is the best method to move .o files to a specified folder?

BIN = bin/
OBJ = obj/
TARGET = opengl_03
DEPS = main.o  displayinit.o initializer.o algorithms.o matrix3f.o window.o vertex3.o
CC = g++
CFLAGS = -g 
LIBS = -lglut -lGLEW -lGL 
INCLUDEPATH = -L/usr/include/ -L/usr/lib/ -L/usr/lib/x86_64-linux-gnu/

$(TARGET) : $(DEPS)
    $(CC) $(CFLAGS) -o $(BIN)$(TARGET) $(DEPS) $(LIBS) $(INCLUDEPATH) 

displayinit.o : displayinit.cpp displayinit.h
    $(CC) $(LIBS) $(INCLUDEPATH) -c displayinit.cpp && mv displayinit.o $(OBJ)displayinit.o
initializer.o : initializer.cpp initializer.h
    $(CC) $(LIBS) $(INCLUDEPATH) -c initializer.cpp $(OBJ)
algorithms.o : algorithms.cpp algorithms.h
    $(CC) -c algorithms.cpp $(OBJ)
matrix3f.o : matrix3f.cpp matrix3f.h
    $(CC) $(LIBS) $(INCLUDEPATH)  -c matrix3f.cpp $(OBJ)
vertex3.o : vertex3.cpp vertex3.h
    $(CC) $(LIBS) $(INCLUDEPATH)  -c vertex3.cpp $(OBJ)
window.o : window.cpp window.h
    $(CC) $(LIBS) $(INCLUDEPATH) -c window.cpp $(OBJ)
main.o : main.cpp
    $(CC) $(LIBS) $(INCLUDEPATH) -c main.cpp $(OBJ)
  • 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-27T17:46:41+00:00Added an answer on May 27, 2026 at 5:46 pm

    To specify where the object is created use -o

    window.o : window.cpp window.h
        $(CC) $(LIBS) $(INCLUDEPATH) -c window.cpp -o $(OBJ)/$@
    

    Here is what you could do:

    1. specify the directory where you want the object files to go

      OBJDIR    =   objdir
      
    2. Create a list of object files that need to be compiled, from the list of all .cpp files by replacing .cpp with .o and add the prefix $(OBJDIR)/ to it:

      OBJ = $(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
      

      So your $(OBJ) will look like: objdir/window.o objdir/main.o and so on

    3. Add a target to create the directory if it does not exist:

      $(OBJDIR):
          mkdir $(OBJDIR)
      
    4. Make the directory target before you make your main target:

      all: $(OBJDIR) myapp
      
    5. Rule to compile all the .o object files in $(OBJDIR) from .cpp files in the current directory:

      $(OBJDIR)/%.o: %.cpp
          $(GCC) $(CPPFLAGS) -c $< -o $@
      

      This will result in something like:

      g++ -c main.cpp -o objdir/main.o
      
    6. Your main target is unchanged:

      $(TARGET): $(OBJ)
          $(GCC) $(LDFLAGS) -o $@ $^ 
      

      This will look like:

      g++  -o myapp objdir/window.o objdir/main.o 
      
    7. For completeness add clean target to cleanup objects:

      clean:
          @rm -f $(TARGET) $(wildcard *.o)
          @rm -rf $(OBJDIR) 
      
    8. And define .PHONY targets, e.g. these will be made even if directories or files with the same name exist:

      .PHONY: all clean
      

    So it should look like:

    OBJDIR    =   objdir
    OBJ       =   $(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
    TARGET    =   my app
    
    .PHONY: all clean
    
    all: $(OBJDIR) $(TARGET)
    
    $(OBJDIR):
        mkdir $(OBJDIR)
    
    $(OBJDIR)/%.o: %.cpp
        $(GCC) $(CPPFLAGS) -c $< -o $@
    
    $(TARGET): $(OBJ)
        $(GCC) $(LDFLAGS) -o $@ $^ 
    
    clean:
        @rm -f $(TARGET) $(wildcard *.o)
        @rm -rf $(OBJDIR) 
    

    And if you have files such as main.cpp and a.cpp this is what make would do:

    > ls
    Makefile a.cpp    main.cpp
    
    > make
    mkdir objdir
    g++ -I. -c a.cpp -o objdir/a.o
    g++ -I. -c main.cpp -o objdir/main.o
    g++ -o myapp objdir/a.o objdir/main.o 
    
    > ls
    Makefile a.cpp    main.cpp objdir   myapp
    
    > make clean
    > ls
    Makefile a.cpp    main.cpp
    

    And if you want to read more details about any of the above have a look at GNU make doc page

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I want to construct a data frame in an Rcpp function, but when I
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have thousands of HTML files to process using Groovy/Java and I need to

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.