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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:22:55+00:00 2026-06-13T03:22:55+00:00

I want to build an app and I have multiple modules stored in multiple

  • 0

I want to build an app and I have multiple modules stored in multiple directories. I’ve decided to follow this idea, i.e. to have a makefile in each directory and then to merge it. But – as a beginner programmer – I still do not see how to do that. First of all, how would such “partial” makefiles look like. They cannot have main function as there can be only one per binary, though when I try to compile it gcc complains for the undefined reference to main. Secondly, I have no idea how would putting all those modules together look like.

I would appreciate any help, but please try to keep your answers simple. Makefiles are still a bit of black magic to me.

  • 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-13T03:22:56+00:00Added an answer on June 13, 2026 at 3:22 am

    Before you can do anything with a makefile, you must know how to do it without a makefile.

    Since you are using gcc, I will assume that your source code is C++.

    You haven’t told us what your directory structure looks like, so I’ll suppose that you have three source files in two directories: primary/main.cc, other/foo.cc and other/bar.cc. (We can deal with header files like foo.h later.) And you want to build myApp.

    STEP 1: Doing It By Hand

    To do this in one command, you might use:

    gcc -Wall primary/main.cc other/foo.cc other/bar.cc -o myApp
    

    This will compile the three source files and link the binary objects together into the executable myApp.

    STEP 2: Doing It In Pieces (Do not attempt this until you can get the previous step to work perfectly.)

    Instead of building with one command, you could take an intermediate step, compiling the source files into binary object files:

    gcc -Wall -c primary/main.cc -o primary/main.o
    gcc -Wall -c other/foo.cc -o other/foo.o
    gcc -Wall -c other/bar.cc -o other/bar.o
    

    This will produce alpha/main.o, beta/foo.o and beta/bar.o. The compiler won’t complain about foo and bar lacking a main() function, because an object file doesn’t need one. Then link the objects together into an executable:

    gcc -Wall primary/main.o other/foo.o other/bar.o -o myApp
    

    STEP 3: Doing It Locally (Do not attempt this until you can get the previous step to work perfectly.)

    Just like the previous step, but we act in primary/ and other/:

    cd primary
    gcc -Wall -c main.cc -o main.o
    cd ../other
    gcc -Wall -c foo.cc -o foo.o
    gcc -Wall -c bar.cc -o bar.o
    cd ..
    gcc -Wall primary/main.o other/foo.o other/bar.o -o myApp
    

    STEP 4: Using a Makefile (Do not attempt this until you can get the previous step to work perfectly.)

    We could have a makefile perform STEP 1, but that isn’t really necessary. Write a makefile in primary (i.e. primary/makefile) like this:

    main.o:
        gcc -Wall -c main.cc -o main.o
    

    (That whitespace in fromt of gcc... is a TAB.)

    Now try this:

    cd primary
    make
    cd ../other
    gcc -Wall -c foo.cc -o foo.o
    gcc -Wall -c bar.cc -o bar.o
    cd ..
    gcc -Wall primary/main.o other/foo.o other/bar.o -o myApp
    

    STEP 5: Using Several Makefiles (Do not attempt this until you can get the previous step to work perfectly.)

    Write a other/makefile:

    both: foo.o bar.o
    
    foo.o:
        gcc -Wall -c foo.cc -o foo.o
    
    bar.o:
        gcc -Wall -c bar.cc -o bar.o
    

    and a makefile in the top directory, where you’re building myApp:

    myApp:
        gcc -Wall primary/main.o other/foo.o other/bar.o -o myApp
    

    Now try this:

    cd primary
    make
    cd ../other
    make
    cd ..
    make
    

    STEP 6: Using One Makefile That Calls Others (Do not attempt this until you can get the previous step to work perfectly.)

    Edit the top makefile:

    myApp:
        cd primary; make
        cd other; make
        gcc -Wall primary/main.o other/foo.o other/bar.o -o myApp
    

    Now try:

    make
    

    If all of this works, what you have is a crude but effective makefile system. There are many refinements possible, when you’re ready to take the training wheels off.

    EDIT:

    If there are many source files in a subdirectory (e.g. other/) and you don’t want to maintain a list in the top makefile by hand, there are several ways to handle it. This is one:

    OTHER_SOURCES := $(wildcard other/*.cc)
    
    OTHER_OBJECTS := $(OTHER_SOURCES:.cc=.o)
    
    myApp:
        cd primary; make
        cd other; make
        gcc -Wall primary/main.o $(OTHER_OBJECTS) -o myApp
    

    But you should get these makefiles working and understand them, before you try any more streamlining.

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

Sidebar

Related Questions

in my app i have activity with XML layout, i want to build an
I want to build a REST web service on app engine. Currently i have
I want build a sketch pad app on iPhone, I assume that this type
I have an app that I want to be able to build two different
i want build a photography app with effects . e.g. old images with brown
I want to build an app that works like custom URL app where I
I want to build a flash app to let me and my users snap
I want to build a FB app which posts messages to the walls of
I want to build a Java web app and deploy it on EC2 .
I want to build my first mobile app. I am wondering if I should

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.