Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-13-2012, 07:09 AM   #1
Shahid nx
Registered: Jan 2012
Posts: 46

Rep: Reputation: Disabled
creating more than one in single makefile in c?

Hi all,
what is way to create two executable from two programs in a single make file. i.e i have programs abc.c and xyz.c and i m creating the makefile like this
gcc xyz.o -o xyz
gcc abc.o -o abc
gcc abc.c -c
gcc xyz.c -c
But when i execute make command it is only creating the xyz executable not creating abc.!! Can anyone please explain me why this is happening? and also solution for this.

shahid nx
Old 10-13-2012, 09:37 AM   #2
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Essentially the problem with yours is that when you type 'make' it will run the first rule, which is

gcc xyz.o -o xyz
What you need instead is

all: xyz.o abc.o
  gcc xyz.o abc.o -o executable
However, you could also do is:

OBJECTS = abc.o xyz.o

all: $(OBJECTS)
    gcc $(OBJECTS) -o executable

%.o: %.c
    gcc -c $<
So the bottom bit is a rule to change a .c file to a .o file, the middle bit says that to make the executable you need objects 'abc.o' and 'xyz.o' and how to combine them to make the executable using gcc, and the first line lists all the objects you need.

Hope this helps,

Last edited by Snark1994; 10-13-2012 at 09:39 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a Makefile BackwardsDown Programming 1 11-26-2006 01:47 PM
Compiling multiple .c file using single command in makefile vipulc Linux - General 2 03-19-2006 12:49 AM
Creating a makefile Spike5 Solaris / OpenSolaris 1 11-24-2005 04:46 PM
creating a Makefile true_atlantis Programming 3 02-26-2004 09:35 PM
Creating a Makefile-GTK The Yeti Lives Linux - Newbie 0 06-14-2003 07:56 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration