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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:23:45+00:00 2026-06-16T02:23:45+00:00

I’m a beginner. I’d like to create a makefile for C programs in Linux.

  • 0

I’m a beginner. I’d like to create a makefile for C programs in Linux.

My program flow is,
cmd.c calls cdirec(), dspecd(), sfile(), hfile() which are declared in mainl.h (but not defined in it)

cdirec(), dspecd(), sfile() and hfile() are defined in cdirec.c, dspecd.c, sfile.c and hfile.c respectively.

cdirec() in turn calls mimx(), skloc(), fnc() and fcmp() which are declared in subl.h (but not defined in it)

mimx(), skloc(), fnc() and fcmp() are defined in mimx.c, skloc.c, fnc.c and fcmp.c respectively.

I tried to create a makefile as follows,

all: cmd.o cdir.o dspecd.o sfile.o hfile.o
    cc cmd.o cdir.o dspecd.o sfile.o hfile.o -o sourceinfo
cmd.o: cmd.c mainl.h
    cc -c cmd.c
cdir.o: cdirec.o mimx.o skloc.o fnc.o fcmp.o
    cc cdirec.o mimx.o skloc.o fnc.o fcmp.o -o cdir.o
cdirec.o: cdirec.c mainl.h subl.h
    cc -c cdirec.c
dspecd.o: dspecd.c mainl.h
    cc -c dspecd.c
sfile.o: sfile.c mainl.h
    cc -c sfile.c
hfile.o: hfile.c mainl.h
    cc -c hfile.c
mimx.o: mimx.c subl.h
    cc -c mimx.c
skloc.o: skloc.c subl.h
    cc -c skloc.c
fnc.o: fnc.c subl.h
    cc -c fnc.c
fcmp.o: fcmp.c subl.h
    cc -c fcmp.c

I don’t understand what the problem is. But I get an undefined reference to main in cdir.o. cdirec.c looks like this,

#include<stdio.h>
#include "subl.h"

void cdirec()
{
printf("\nAll details of current directory is printed.\n");
mimx();
skloc();
fnc();
fcmp();
}

I don’t and can’t have a main in it, as it is just a function declaration file.
Kindly guide me.

ANSWER:

all: cmd.o cdirec.o dspecd.o sfile.o hfile.o mimx.o skloc.o fnc.o fcmp.o 
    cc cmd.o cdirec.o dspecd.o sfile.o hfile.o mimx.o skloc.o fnc.o fcmp.o -o sourceinfo
cmd.o: cmd.c mainl.h
    cc -c cmd.c
cdirec.o: cdirec.c mainl.h subl.h
    cc -c cdirec.c
dspecd.o: dspecd.c mainl.h
    cc -c dspecd.c
sfile.o: sfile.c mainl.h
    cc -c sfile.c
hfile.o: hfile.c mainl.h
    cc -c hfile.c
mimx.o: mimx.c subl.h
    cc -c mimx.c
skloc.o: skloc.c subl.h
    cc -c skloc.c
fnc.o: fnc.c subl.h
    cc -c fnc.c
fcmp.o: fcmp.c subl.h
    cc -c fcmp.c
clean:
    rm -rf *.o sourceinfo
  • 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-16T02:23:47+00:00Added an answer on June 16, 2026 at 2:23 am

    The problem occurs here:

    cdir.o: cdirec.o mimx.o skloc.o fnc.o fcmp.o
        cc cdirec.o mimx.o skloc.o fnc.o fcmp.o -o cdir.o
    

    This is telling cc to link these given .o files into an executable named cdir.o. An executable needs a main function. But it looks like you’re not trying to link an executable; instead, you’re trying to collect multiple object files into a single object file.

    You don’t need to do this; you can just link all of your object files together in the final executable. If you just find it cumbersome to deal with that many files on one line, you can define a Make variable; a common name is OBJS (short for “objects”):

    OBJS  = cdirec.o 
    OBJS += mimx.o 
    OBJS += skloc.o 
    OBJS += fnc.o 
    OBJS += fcmp.o 
    # ... etc
    
    sourceinfo: ${OBJS}
        cc ${OBJS} -o sourceinfo
    

    If you want to collect them into a single file, you can build a library, and then link that into the final executable. A library is built with the ar command; it should have the extension .a:

    cdir.a: cdirec.o mimx.o skloc.o fnc.o fcmp.o
        ar rcs cdir.a cdirec.o mimx.o skloc.o fnc.o fcmp.o
    

    You then link that when building your final executable:

    cc cmd.o dspecd.o sfile.o hfile.o cdir.a -o sourceinfo
    

    As an aside on your organization, it looks like you might be defining one function per source file. That gets pretty cumbersome pretty quick. You usually group related functions together in the same source file. It’s possible that all of cdirec(), mimx() skloc(), fnc() and fcmp() should be defined in the same file, so that they produce a single object file, though I can’t say for sure without seeing your actual code.

    There are also further features in Make that would make your Makefile much simpler. In particular, there are already implicit rules for compiling .c files into .o, so you don’t need all those rules, just the dependencies on the headers (because those aren’t generated implicitly):

    cmd.o: mainl.h
    cdirec.o: mainl.h subl.h
    dspecd.o: mainl.h
    sfile.o: mainl.h
    hfile.o: mainl.h
    mimx.o: subl.h
    skloc.o: subl.h
    fnc.o: subl.h
    fcmp.o: subl.h
    

    You can actually generate these dependencies automatically as well, but that’s a bit more complicated, so I won’t go into it here.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

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.