libcs50 icon indicating copy to clipboard operation
libcs50 copied to clipboard

Makefile overrides local CFLAGS and LDFLAGS

Open lquidfire opened this issue 1 year ago • 0 comments

Not sure where else to ask this question. This is about the manual building of libcs50 on Linux:

The Makefile for libcs50 overrides the local CFLAGS and LDFLAGS. Is this desired behaviour?

The standard flags on Archlinux are currently:

CFLAGS="-march=x86-64 -O2 -pipe -fno-plt -fexceptions \ -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \ -fstack-clash-protection -fcf-protection"

LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now"

By overriding the local flags, libcs50 gets compiled withour RELRO for Archlinux.

Would RELRO (or other local flags) inhibit the functionality of libcs50? If not, would it be an option to not override local flags, but simply add the desired flags from the Makefile to the local flags?

I have applied the following patch to the Makefile to add the flags from Makefile to the local CFLAGS and LDFLAGS, instead of overriding the local flags (Linux only):

--- Makefile    2024-01-17 16:45:50.820032476 +0200
+++ Makefile.old        2024-01-17 16:50:35.989535760 +0200
@@ -9,7 +9,7 @@
 INCLUDE := src/cs50.h
 MANS := $(wildcard docs/*.3.gz)
 
-CFLAGS=-Wall -Wextra -Werror -pedantic -std=c11
+CFLAGS+=-Wall -Wextra -Werror -pedantic -std=c11
 BASENAME=libcs50
 LIB_STATIC=$(BASENAME).a
 LIB_OBJ=$(BASENAME).o
@@ -36,8 +36,8 @@
 all: $(LIBS) $(MANS)
 
 $(LIBS): $(SRC) $(INCLUDE) Makefile
-       $(CC) $(CFLAGS) -fPIC -shared $(LINKER_FLAGS) -o $(LIB_VERSION) $(SRC)
-       $(CC) $(CFLAGS) -c -o $(LIB_OBJ) $(SRC)
+       $(CC) $(CFLAGS) -fPIC -shared $(LINKER_FLAGS) -o $(LIB_VERSION) $(SRC) ${LDFLAGS}
+       $(CC) $(CFLAGS) -c -o $(LIB_OBJ) $(SRC) $(LDFLAGS)
        ar rcs $(LIB_STATIC) $(LIB_OBJ)
        chmod 644 $(LIB_STATIC)
        rm -f $(LIB_OBJ)

lquidfire avatar Jan 19 '24 08:01 lquidfire