Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer the question correctly and show the correct code and show that it runs. Thank you. IN C PROGRAM Make files (about 45-60 minutes)

Please answer the question correctly and show the correct code and show that it runs. Thank you. IN C PROGRAM

Make files (about 45-60 minutes) Purpose: Creating a make file Files: Download the tutorial tar file t9_make.tar and extract the files into the tutorial directory. As programs get larger and include multiple files, the management of the files and the executable is getting harder. Often it is hard to keep track of all the files that were changed. As a result if changed files are not compiled and relinked then the changes are not included in the executable. One option is to recompile and link all the files. However, this may time consuming. For example, if a program includes 50 *.c files and each file compilation takes 3 seconds then one has to wait 150 seconds or about 3 minutes before one can test the program again. Unix has a utility that assists in the management of the files the make utility. The make utility is designed to help determine which files should be recompiled and relinked into the program. The make utility reads an input file, which by default is named Makefile, to manage the code. The purpose of the Makefile is to maintain the decency between files. For example, if FileA depends on FileB then whenever a change occurs in FileB then FileA must be recompiled. For example, file main_test.c contains test program for a linked list and file linked_list.c contains the linked list functions. In this case if there are changes to the linked list code then the file main_test.c may need to be recompiled and linked with the updated linked_list.c file. 2 The dependencies are listed in the make file as follows: File name: list of dependencies In the above example the dependency will appear in the file as FileA : FileB FileA, which appear before the : is termed the target file and FileB is termed the dependency file list. Next, one needs to determine the action that must be taken when a change has occurred in one of the files (e.g., FileB above) The action is written below the list of dependencies. For example if FileB has changed then a new executable is created using gcc o FileA FileB This will appear in the file as follows: FileA : FileB gcc o FileA FileB The symbol represents a hard tab. This is a must because the make utility uses the to determine the command/rule to be invoked. Note that this allows one to create a sequence of commands. For example FileA : FileB command 1 command 2 Note that if vim converts the tabs to spaces then open the file .vimrc and comment out the line set expandtab. Commenting is done by insert a as the first character. FileA is termed the target file. The compiling instruction gcc is termed the rule or command. In this tutorial you have three .c files (person.c main.c and mystat.c) and two .h files (mystat.h and person.h). 1. What is the dependency of each of the .c files? Each file is dependent on the .h files that are included in the file. For example mystat.c depends on mystat.h 1.1. Determine for each .c file which files it is dependent on 1.2. Add the dependencies in a file called Makefile. One target per line and at least one line spacing two separate targets. 3 2.1 Creating the rule for person.c The file person.c depends on person.h. The reasons are: 1. person.h contains the data structure required by person.c. Thus, if the data structure was modified then the code should be aware of it; and 2. Person.h contains the prototypes of the functions in person.c. Thus, if the functions parameter were changed then they should be updated in the person.h. 1.3. Creating the rule for person.c Add to the file Makefile the following person.c: person.h mystat.h gcc person.c Note the rule was indented using the tab. 1.4. Modify the file person.h in order to ensure that it is newer than person.c. Here you can either use the touch command as touch person.h or modify person.h and save it (e.g., by adding a space to person.h 1.5. Execute the make utility by typing make in the command line. The make utility will look for the default file name Makefile. If you are using another file (e.g., myMakefile) then you invoke the make tool as follows make -f myMakefile. What is the outcome? Where there any errors? Why did the errors occur? The errors occurred because the file person.c does not have all the needed code to compile and link it into an executable (e.g., it does not have a main() function). Therefore, the command gcc person.c was not correct. It should be modified to create an object file. Change the rule to gcc c person.c 1.6. Invoke the make utility again by typing make. What is the outcome? What file was created? 1.7. Invoke the make utility again. What happened? It seems that the file person.c was compiled again even though the file person.c and person.h. Why did it happen? The file was compiled again because the target, which is person.c is not affected by the rule. That means that we used the wrong target for the file person.c. Change the target to person.o instead of person.c and update the dependency list to include person.c person.o: person.h person.c mystat.h gcc -c person.c 1.8. Delete the file person.o and invoke the make utility again by typing make. What is the outcome? What file was created? 1.9. Invoke the make utility again. What happened? It seems that the target person.o is newer than person.c and person.h and therefore the file person.c was not compiled again. Thus, it is 4 important when one creates a target and a rule in a make file is to ensure that the target is affected by the rule.

PERSON.H

#ifndef HEADER_PERSON #define HEADER_PERSON

#define NAME_LENGTH 16 #define TWO_NAME_LENGTH 32 #define NUM_YEARS 5

struct person { unsigned int id; unsigned short age; float salary[NUM_YEARS]; char first_name[NAME_LENGTH]; char last_name[NAME_LENGTH]; };

void populate(struct person *p); void print(struct person *p);

#endif

PERSON.C

#include #include #include

#include "person.h" #include "mystat.h"

#define BASE_SALARY 30000

// Print the information of a person. void print(struct person *p) { float avg_best_five_years = 0; char s[TWO_NAME_LENGTH];

average(p->salary, NUM_YEARS, &avg_best_five_years); sprintf(s,"%s %s",p->first_name,p->last_name); printf("Person Name: %-32s, Age:%3hu, salary average: %7.2f ", s, p->age, avg_best_five_years); }

// Populate the information of a person. void populate(struct person *p) {

int i; static long long id = 0; char *first_name[5] = { "David", "John", "Diana", "Rob", "Sarah" }; char *last_name[5] = { "Smith", "Bell", "West", "Johnson", "Ford" };

strncpy(p->last_name, last_name[rand() % 5], NAME_LENGTH-1); strncpy(p->first_name, first_name[rand() % 5], NAME_LENGTH-1);

p->id = id++; p->age = (rand() % 45) + 20;

// Populate the salary information. for (i = 0; i < NUM_YEARS; i++) { p->salary[i] = BASE_SALARY + (rand() & 5000); } }

MYSTAT.H

#ifndef HEADER_MYSTAT #define HEADER_MYSTAT

void average(float *arr, int size, float *average);

#endif

MYSTAT.C

#include #include

#include "mystat.h"

// Compute the average of an array of floats. void average(float *arr, int size, float *average) { int i; *average = 0;

for (i = 0; i < size; i++, arr++) { *average += *arr; } *average /= size; }

MAIN.C

#include #include #include

#include "person.h"

#define NUM_PEOPLE 5

int main(int argc, char *argv[]) { struct person people[NUM_PEOPLE]; int i;

for (i = 0; i < NUM_PEOPLE; i++) { populate(&people[i]); }

for (i = 0; i < NUM_PEOPLE; i++) { print(&people[i]); }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

What is management growth? What are its factors

Answered: 1 week ago