What you want to do, is create a custom Makefile
. A Makefile
is a file you have inside the same directory with your source code, that gets executed when you are in that directory, you run make something
and that something is a target specified inside the Makefile
. Let's see how to create such a file.
Start by specifying the compiler yo use, in your case clang
.
CC = clang
Then the flags to pass the compiler. Those used in CS50 are:
CFLAGS = -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror
Then the name of the executable to be created.
EXE = greedy
Then a space separated list of all the headers your program will use (none in your case).
HDRS =
Then a space separated list of all the libraries your program will use.
LIBS = -lcs50 -lm
Then a space-separated list of C files that will collectively implement your program.
SRCS = greedy.c
Then the same list in SRCS
but instead of .c
with .o
.
OBJS = $(SRCS:.c=.o)
Then the target line, where you specify what will happen exactly when the command is given.
$(EXE): $(OBJS) $(HDRS) Makefile
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS)
Then the files that your .o
files "depend on".
$(OBJS): $(HDRS) Makefile
And then a command for housekeeping, that will remove any core
, compiled and .o
files you have in the folder by previous compilations or runs.
clean:
rm -f core $(EXE) *.o
Keep in mind that the indentations in the commands, are tab
characters and not spaces. So make sure your editor doesn't change the tabs into spaces automatically as many do.
So your final file (Called Makefile
) must look something like this:
CC = clang
CFLAGS = -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror
EXE = greedy
HDRS =
LIBS = -lcs50 -lm
SRCS = greedy.c
OBJS = $(SRCS:.c=.o)
$(EXE): $(OBJS) $(HDRS) Makefile
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS)
$(OBJS): $(HDRS) Makefile
clean:
rm -f core $(EXE) *.o
If you want to make a Makefile
for another project just change the parameters that change. You can learn more about Makefiles here: http://cdn.cs50.net/2015/x/psets/5/pset5/pset5.html#makefile. If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.
[I should note that I posted this late answer as I didn't like very much @wallek876 's solution with the aliases. It could also be a reference for how to create a Makefile as I haven't seen one here.]
main': entername.c:(.text+0x18): undefined reference to
GetString' collect2: error: ld returned 1 exit status make: *** [entername] Error 1