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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:12:24+00:00 2026-06-01T19:12:24+00:00

I am stuck in writing a Makefile when my source code files are across

  • 0

I am stuck in writing a Makefile when my source code files are across different directories.

The directory structure is as follows :

I have my source files (.cc) in the folders FOLDER1 and FOLDER2 and the header files are in folder named INCLUDE. My makefile is present in FOLDER1.

       program_NAME := myprogram
       program_C_SRCS := $(wildcard *.cc)
       program_C_OBJS := ${program_C_SRCS:.cc=.o}
       program_OBJS := $(program_C_OBJS) 
       program_INCLUDE_DIRS := ../INCLUDE
       program_LIBRARY_DIRS := 
       program_LIBRARIES :=
       CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
       LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
       LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))

       .PHONY: all clean distclean

       all: $(program_NAME)

       $(program_NAME): $(program_OBJS)
       $(LINK.cc) $(program_OBJS) -o $(program_NAME)

       clean:
           @- $(RM) $(program_NAME)
           @- $(RM) $(program_OBJS)

       distclean: clean

Here if I keep all my source (.cc) files in FOLDER1 then it works but on moving some files to FOLDER2 it gives errors of undefined reference.

Please help me understand how to modify my makefile so that I can keep all my header files in one directory say INCLUDE and distribute my source files across different directories.

Thanks !!!

  • 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-01T19:12:25+00:00Added an answer on June 1, 2026 at 7:12 pm

    The problem is, that

    program_C_SRCS := $(wildcard *.cc)
    

    only adds source files in the same directory. So when linking you don’t have the object files of your second folder. You probably can solve the problem by simply adding the source files of the other folder to your program_C_SRCS:

    program_C_SRCS += $(wildcard ../FOLDER2/*.cc)
    

    Include directorys

    I guess you are using the gcc/g++, in this case http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html says, that header paths are searched from left to right, meaning the first path given is searched first. Therefore you only have to add -I INCLUDE2 before -I INCLUDE1 option to achive what you want.
    Your starting makefile already has some transformation for more then one include path build in:

       CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
    

    So you only have to rewrite your include paths:

    program_INCLUDE_DIRS := ../INCLUDE2
    program_INCLUDE_DIRS += ../INCLUDE1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Fellow stackoverflowers, I´m stuck writing a piece of code. I have application with
How I'm stuck with writing a test for the following code. I want to
I'm writing some C code to parse IEEE 802.11 frames, but I'm stuck trying
I'm stuck writing jQuery validation for a contact form. I wrote a simple code
I've gotten stuck writing some parallel c code using OpenMP for a concurrency course.
Hi guys i'm almost done writing my code and i'm stuck with this stupid
I'm kinda stuck in writing Stored Procedure. Here is the case. I have one
I'm writing a code generator and am getting stuck on determining the nullable status
All, I'm writing a native application for Android and i'm stuck trying to acquire
Kinda stuck here... I have an application with lets say 5000 rows of data

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.