Thread: Creating makefiles

  1. #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

  2. #2
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I found the answer.

  3. #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.

  4. #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.

  5. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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