cs50/libcs50

How does the make command automatically link to the cs50 library or the libraries used?

minh-cong opened this issue · 3 comments

I've been struggling with it all day and just found a way to create a makefile. But folders that do not contain makefile files will not automatically link to the cs50 library.

Thank you professor

just saving for myself:

MAKEFLAGS += --debug=why
# MAKEFLAGS += --no-silent
# MAKEFLAGS += --warn-undefined-variables


CC := clang
CFLAGS := -std=c11

# Below are common C[XX]FLAGS
CCFLAGS += -ferror-limit=1 -gdwarf-4 -ggdb3 -O0
CCFLAGS += -Wall -Werror -Wextra
CCFLAGS += -Wno-gnu-folding-constant -Wno-sign-compare
CCFLAGS += -Wno-unused-parameter -Wno-unused-variable -Wno-unused-but-set-variable
CCFLAGS += -Wshadow

CFLAGS += $(CCFLAGS)

# LDLIBS := -lcrypt -lcs50 -lm
LDLIBS := -lm


# Following is for use with IDEs like Geany, Kate, Eclipse, IntelliJ, PyCharm, etc
.PHONY: all

all: # <pset_file_basename_i.e._without_extension>


# EXE = foo

# SRCS = foo.c bar.c
# OBJS = $(SRCS:.c=.o)


# $(EXE): $(OBJS)
# 	$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDLIBS)


# https://cs50.readthedocs.io/libraries/cs50/c/#makefile
# https://github.com/cs50/libcs50/issues/287

for using on windows, as these vars are essentially common to all the cs50x psets based on c-lang so, just cmd /c mklink /h Makefile <pset>\Makefile (ref: howtogeek/win-symlink)


References:

  • Implicit Variables to make https://www.gnu.org/software/make/manual/make.html#Implicit-Variables

  • --debug=why shows following output:

    $ make --debug=why inheritance
    <builtin>: update target 'inheritance' due to: inheritance.c
  • MAKEFLAGS variable:

    declare mk="$(curl "https://www.gnu.org/software/make/manual/make.txt")"

    then do:

    tac - <<< "$mk" | grep flags -m7 -A4 | tac
    #                                 ^~~~ A(fter) since `tac`ed

    Or equivalently:

    echo "$mk" | bat   # then do: `?flags` and hope for it to wrap back and show search results from last