GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-May-2009, 05:18
lanz lanz is offline
New Member
 
Join Date: Oct 2007
Posts: 21
lanz is on a distinguished road

Help with creating a makefile


Hi there!

I've just been looking around on the forum and was hoping to learn/see the output of the two files in this tutorial:
Code:
http://www.gidforums.com/t-3369.html
through the use of a makefile.

Was wondering how to do this and how I would 'compile'?

Looking around, I've come up with this:
Code:
#file: makefile a.out: parent.o child.o cc parent.o child.o parent.o: parent.c cc -c parent.c child.o: child.c cc -c child.c
I use cc to compile my little programs.

Advanced thank you.
  #2  
Old 27-May-2009, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with creating a makefile


Quote:
Originally Posted by lanz
...how to do this...

With a text editor, create a file named Makefile in the same directory as parent.c and child.c. Paste the contents of your post into that file.
Quote:
Originally Posted by lanz
and how I would 'compile'?

1. from the command line enter:

Code:
make a.out

or...

2. Since the first target will be "made" if no arguments are given, simply enter:

Code:
make

3. Execute the executable. If this is Linux or some other UNIX-like operating system, then enter the following from the command line:

Code:
./a.out

Regards,

Dave
  #3  
Old 27-May-2009, 12:00
lanz lanz is offline
New Member
 
Join Date: Oct 2007
Posts: 21
lanz is on a distinguished road

Re: Help with creating a makefile


Hi Dave

Thanks a bunch! =] I feel very noob-y at the moment. =p

Ani
  #4  
Old 27-May-2009, 12:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with creating a makefile


Quote:
Originally Posted by lanz
...noob-y
Well, no one was born knowing this stuff...

Regards,

Dave

"I was born not knowing, and have only had
a little time to change that here and there."
---Richard Feynman (1918-1988)
  #5  
Old 28-May-2009, 11:13
lanz lanz is offline
New Member
 
Join Date: Oct 2007
Posts: 21
lanz is on a distinguished road

Re: Help with creating a makefile


^^, That's true I guess.

Just wondering - A bit off topic but I'm trying to get the tutorial working however I keep getting this error when executing:
Code:
[lanz@han ~/Desktop]$ ./a.out execl Error! Broken pipe

the code is pretty much the same except I changed the int main() in the child code so that it looks like this:
child.c
CPP / C++ / C Code:
#include <stdio.h>

int child(){
	char string[100];
	
	printf("Child Process\n");
	printf("-------------\n");
	do{
		printf("Enter Command: ");
		fflush(stdout);				/* Must flush to see command prompt */
		fgets(string,100,stdin);
		printf("%s\n",string);		/* No flush necessary because new line flushes */
	}while(!strstr(string,"exit"));
	
	return 0;
}
Because of the error I was getting that main() was being redeclared.

Could it be because I'm compiling on the desktop? (Both files are in the same place)
  #6  
Old 28-May-2009, 14:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with creating a makefile


Quote:
Originally Posted by lanz
Because of the error I was getting that main() was being redeclared.
I wasn't actually looking at the applications. I just showed how to use your makefile to compile two different files into a single program. This is the kind of thing you do when some functions of the program are in one file and other functions are in the other.

However...

The example that you are working with has two separate programs in two separate files. Each program has a main() function.

Bottom line: If both files have main(), you can't compile them together (and there is absolutely no reason that I can think of for wanting to do such a thing anyhow.)

Compile parent.c by itself. Compile child.c by itself. Then invoke the parent.

Here's a very simple Makefile.
Code:
# Very naive Makefile for two separate programs. all: parent child parent: parent.c cc parent.c -o parent child: child.c cc child.c -o child clean: rm -f parent child

Since each program has only one file, there is really no need to create the .o files. Just compile each one to its own executable. Note that I always include a "clean" target so that you can start "fresh" with "make clean"


Here's my suggestion:

In your home directory, create a new directory named example1. Do it from a command line:

Code:
cd mkdir example1


Copy your original parent.c and child.c into the example1 directory.
Create the new Makefile that I just showed and put it in the example directory.

Then, from a command line (in the example1 directory), execute
Code:
make ./parent

Regards,

Dave

Quote:
Originally Posted by lanz
I changed the int main() in the child code so that it looks like this

Now you have a single program with source code in two separate files. Your original Makefile will compile the individual functions in the two files and link them together.

However the example won't work, since it expects there to be a program named "child" that was compiled from the original child.c


Regards,

Dave
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error compiling Apache 2.2.10 tiagofgarcia Apache Web Server Forum 2 10-Nov-2009 14:31
Need help with makefile New_Programmer C++ Forum 2 31-Mar-2009 23:49
Problem executing nam-1.13 RodolfoAlvizu Computer Software Forum - Linux 20 28-Feb-2009 16:23
Mixing C and assembly in x86 - Makefile nuances aijazbaig1 Assembly Language 3 23-Apr-2008 09:29
I need some help with my makefile program takachi C Programming Language 2 14-Jan-2008 06:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.