Using the Make command

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

File hello.h

#include <stdio.h>

File main.c

#include "hello.h"
 
main()
{
    printhello();
    credits();
}

File print.c

#include "hello.h"
 
void printhello()
{
    printf ("Hello world!\n");
}

File credits.c

#include "hello.h"
 
void credits()
{
    printf ("\n(This ridiculous example of overkill was created for CptS 224.)\n");
}

(6 points) Write a Makefile that will correctly compile subparts "main.o", "print.o", "credits.o" and both "make helloworld" command and "make" commmand will assemble a program called "helloworld" from those parts. The dependencies should be correct so that if any of the 4 files is updated, the correct pieces will get rebuilt.
(1 point) Your Makefile should be written so that "make clean" command will remove program "helloworld", "*.o" and any other executables which were created.

those are the two i need help with the most. It doesnt seem like i am doing it correctly

  1. Relevant commands, code, scripts, algorithms:

make make clean ls

  1. The attempts at a solution (include all code and scripts):
9:58pm
aaron@aaron-VirtualBox:~/Desktop$ cc -c hello.h

aaron@aaron-VirtualBox:~/Desktop$ cc -c main.c

aaron@aaron-VirtualBox:~/Desktop$ cc -c print.c

aaron@aaron-VirtualBox:~/Desktop$ cc -c credits.c

aaron@aaron-VirtualBox:~/Desktop$ cc hello.h main.o print.o credits.o -o helloworld

aaron@aaron-VirtualBox:~/Desktop$ ./helloworld

Hello world!



(This ridiculous example of overkill was created for CptS 224.)

aaron@aaron-VirtualBox:~/Desktop$ 
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Washington State University, Washington, US, Bakken, CS224 CptS 224: Homework 3 - Make

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Makefile Tutorial Should be able to help with no problems.

Maybe a short explanation will get you up to speed faster.

The "make" utility is intended for repetitive tasks (like compiling/linking). It works rule-based. In its input file there is a collection of such rules laid out.

Rules consist of target files, source files and actions. When a source file is changed (=has a time stamp younger than the target file) the action is carried out. Usually carrying out the action creates the target file. The big advantage of using a make-file instead of a script with all the compiler/linker commands is that "make" will skip all the files which are already up to date and only execute the rules necessary to update the files which aren't. It will also stop if one of the commands carried out will return a non-zero error level (compilers do that when encountering severe errors).

You should now be able to understand the manual and work out your little makefile.

I hope this helps.

bakunin

Due to strict policies regarding cheating, I can't tell you exactly, but I'll give an example with the undergrad project I made for a course about data structures.

I had the files

binsearch.c  
bubble.c  
csbubble.c  
heap.c  
hybrid.c  
intsearch.c  
main.c  
Makefile  
quick.c  
sel.c

All .c files but main.c were sorting and finding algorithms. The main.c was obviously the main program which took the measurements and the Makefile is the, well, makefile.

I wrote this:

CC    = gcc
DEBUG    = NO_DEBUG
CFLAGS    = -ggdb -std=c99 -pedantic -Wall -W -Wshadow -Wpointer-arith -Wconversion -D$(DEBUG)
OBJ    = bubble.o csbubble.o quick.o heap.o hybrid.o binsearch.o intsearch.o sel.o

all:    $(OBJ)
    $(CC) $(CFLAGS)  -lm -o main main.c $(OBJ)

clean:
    rm -f        \
    Makefile~    \
    bubble.c~    \
    bubble.o    \
    csbubble.c~    \
    csbubble.o    \
    quick.c~    \
    quick.o        \
    heap.c~        \
    heap.o        \
    hybrid.c~    \
    hybrid.o    \
    binsearch.c~    \
    binsearch.o    \
    intsearch.c~    \
    intsearch.o    \
    sel.c~        \
    sel.o        \
    main.c~        \
    main        \
    core

The CC variable is the compiler name. Set it to cc or gcc.
The DEBUG will set a define flag, forget it
the CFLAGS are the options to the compiler. You will have your own. The most common ones are -W or -Wall, -ggdb if you want debug symbols, --std=c99 sets the language dialect (forget it), -pedantic rejects extensions (like gnu89), the -D(DEBUG) passes the aforementioned DEBUG symbol which you won't use, and the other -Wx flags just forget them.
OBJ variable containes ALL the object code binaries that the main program needs, ie all intermediary files of compilations of bubble.c, csbubble.c etc. ATTENTION: If you forget to include even a single object file, your make will fail! Include all your .c files but main.c with the .o extension, as I did.

Let's focus at the rest lines:
The first field is the target. It's called "all", followed by a colon. If you type "make all", this target will be activated. After a TAB, there is the list of all dependencies. There you will put ANY object file your program will need, that is the $OBJ (either type $OBJ or $(OBJ) doesn't matter).

After these dependencies are satisfied, the next lines will execute, up until the next target.
Make will find no bubble.o So it will compile bubble.c with $CFLAGS to get bubble.o. Then will find no csbubble.o, and it will compile csbubble.c, etc. There is NO NEED to put a target for each .c file, as many students do. Make knows how to invoke gcc.

When Make finishes, it will proceed with the next line.
There it calls the compiler ($CC) with $CFLAGS to compile main.c and link it against the object files ($OBJ) and the math library (-lm, you won't need that). The -o main means "the output will be called main. If you don't specify it, it defaults to a.out (assembler output, for the history).

Next will find the clean: target and stop.

If you invoke make as "make clean", then this target will be fired.
Here I delete all backup files (~) all object files (.o), the main executable and core dump (forget this).
You could just put "rm -f *.o helloworld"

That's all folks!

Welcome to this site, I am a new member since last night. Hope we can share interesting insights as we get along in this forum.