C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-28-2005, 03:17 PM   #1
System.out.println("");
 
Join Date: Jan 2005
Posts: 84
Creating makefiles

I feel so stupid, but I can't create a makefile. I ave this C program titled Beginner.c. All of the makefile tutorials I have seen on the net require you to have object files and all of these headers. Frankly, it's way over my head. Could anyone explain it to me? Here's my source code if you need it to figure out how to make a makefile. It seems silly tomake a makefile for just 1 c file, but we have to do it for a class. thanks for the help. BTW, this code simulates a parallel sort.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>


void childProcess(int currentProcess);
void parentProcess(int numProcesses);
int atoi( const char *str );

int numLines; //number of lines in each  file
int numProcesses; //number of processes entered by the user
int numLinesPerFile;
pid_t pid; //process ID so we know whether it's a child or parent processs
int status; 
char fileNames[] = "abcdefghijklmnopqrstuvwxyz"; //this program can handle up to 26 processes
int main(int argc, char *argv[])
{
                  
    //get the information from the command line
    numLines = atoi(argv[2]);
	numProcesses = atoi(argv[3]);
	numLinesPerFile = numLines / numProcesses;
	//check to make sure the user entered the correct number of arguments. If they didn't, 
	//print an error message and quit     
	if(argc != 4)
	{ 
		printf("\nERROR! Wrong number of arguments.\n\n");
		return 0;
	}
	//print out the parameters the user entered for verification
    //this prints out the filename entered
	fprintf(stdout,"\n");
	fprintf(stdout, argv[1]);
	
	printf("\nNumber of lines: %d\n", numLines);
	printf("Number of processes: %d\n", numProcesses);
	printf("\n\nNumber of lines per file: %d\n", numLinesPerFile);
	
	//arbitrary buffer size of 200 to be sure that the string will fit
	//this will split the file up according to the number of processes
	char buff[200];
	sprintf(buff, "split -l %d %s ", numLinesPerFile, argv[1]);
	system(buff); 
    
	int extraLines = numLines - numLinesPerFile*numProcesses;
	//printf("\n\nThe number of extra lines is: %d", extraLines);
	if(extraLines != 0)
	{		
		char buff[200];
		sprintf(buff, "sort -o xa%c -m xa%c xa%c", fileNames[numProcesses-1], fileNames[numProcesses-1], fileNames[numProcesses] );
		system(buff); 	
		
		sprintf(buff, "rm xa%c", fileNames[numProcesses]);
		system(buff);
	}
	
	
	//this portion of the code will use the fork() to create new processes											                                            
	int i;
	for(i = 0; i < numProcesses; i++)
	{
		//keep up with the current process so we know which file to sort
		int currentProcess = i;
		//fork the current process
		pid = fork();
		if(pid == 0) //i am a child!
		{
			childProcess(currentProcess);
		}
		else //i am your parent!
		{
			//pass the number of processes so the parent knows how many files to merge
			parentProcess(numProcesses);
		}
	}
	
	return 0;      
}//end of main function

	void childProcess(int currentProcess)
	{
		//this portion will sort the file of the child we are on
		char buff[200];
		sprintf(buff, "sort xa%c -n -o xa%c", fileNames[currentProcess], fileNames[currentProcess]);
		system(buff);
		return;
	}//end child function
	
	void parentProcess(int numProcesses)
	{
		printf("\nI AM YOUR FATHER!\n");
		int l;
		//for(l = 0; l < numProcesses; l++)
		//{
			//wait for all of the children to catch up
			wait(&status);
		//}
		int j;
		//arbitrary buffer sizeto hold the entire merge
		char buffer[2000];
		for(j = 0; j < numProcesses; j++)
		{
			//if we are on the first item just take the name xaa
			if(j == 0)
			{
				
				sprintf(buffer, "xa%c ", fileNames[j]);
			}
			//if we aren't on the first file we have to do some concatenation with 
			//the new file name and the old ones
			else
			{
				char newBuffer[200];
				sprintf(newBuffer, "xa%c ", fileNames[j]);
				strcat(buffer, newBuffer);
			}
		}

		//this part actually merges the sorted children's files
		char string[20000];
		sprintf(string, "sort -o outfile -m -n %s", buffer);
		system(string);	
		//remove all of the intermediate files
		//sprintf(string, "rm %s", buffer);
		//system(string);
		return;
	}//end parent function
Loctan is offline   Reply With Quote
Old 09-28-2005, 05:42 PM   #2
System.out.println("");
 
Join Date: Jan 2005
Posts: 84
I found the answer.
Loctan is offline   Reply With Quote
Old 09-28-2005, 06:11 PM   #3
Registered User
 
Jaqui's Avatar
 
Join Date: Feb 2005
Posts: 416
good.

now, just an idea.
post the answer so we have a refference next time someone hits the same problem.
__________________
Quote:
Originally Posted by Jeff Henager
If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.
Jaqui is offline   Reply With Quote
Old 10-05-2005, 02:21 PM   #4
System.out.println("");
 
Join Date: Jan 2005
Posts: 84
Suppose your program is named Beginning.c

Code:
P1: Beginning.o
	gcc Beginning.o -o P1
Beginning.o:Beginning.c
	gcc -c Beginning.c
Type make
Then type ./P1 to execute your program.
Loctan is offline   Reply With Quote
Old 10-06-2005, 12:19 AM   #5
Registered User
 
Jaqui's Avatar
 
Join Date: Feb 2005
Posts: 416
and if beginning.c needed to link to an .so, which your makefile had to create? ( or call the makefile to create it )

[ most likely one of the lessons in the class ]

if your app's main file required other files to be compiled into .obj, and it required linking to a dynamic loading library, what would you need to add to existing makefile to get the executable?

( being nasty and asking for more advanced skills, only to push you into looking at a more realistic model of a makefile )
__________________
Quote:
Originally Posted by Jeff Henager
If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.
Jaqui is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Profiler Valgrind afflictedd2 C++ Programming 4 07-18-2008 09:38 AM
creating an application Cpro C++ Programming 21 05-30-2008 12:21 AM
Creating Custom Controls Grayson_Peddie C# Programming 1 12-29-2003 01:41 PM
programming linux with gcc, emacs, and makefiles Captain Penguin Linux Programming 1 11-02-2002 12:04 PM
problems creating a linked list jamjar C Programming 5 10-23-2002 05:50 AM


All times are GMT -6. The time now is 10:08 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22