Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile for Linux and Mac with address sanitizer

I am trying to write a makefile that I can use on linux and mac that builds with an address sanitizer. This works on my vagrant instance:

CC         = gcc
ASAN_FLAGS = -fsanitize=address -fno-omit-frame-pointer -Wno-format-security
ASAN_LIBS  = -static-libasan
CFLAGS    := -Wall -Werror --std=gnu99 -g3
LDFLAGS   += -lpthread

all: hello

hello: tiny_queue.o hello.o
    $(CC) -o $@ $(CFLAGS) $(ASAN_FLAGS) $(CURL_CFLAGS) $^ $(LDFLAGS) $(CURL_LIBS) $(ASAN_LIBS)

This works on ubuntu/trusty64 but fails on my mac with

$ make
gcc -Wall -Werror --std=gnu99 -g3 -I/opt/X11/include  -c -o hello.o hello.c
gcc -o hello -Wall -Werror --std=gnu99 -g3 -fsanitize=address -fno-omit-frame-pointer -Wno-format-security  tiny_queue.o hello.o -lpthread  -static-libasan
clang: error: unknown argument: '-static-libasan'
make: *** [hello] Error 1

Does anyone know how to write a compatible makefile for the mac and linux?

p.s. I'm very new to C, sorry if this question is super basic.

like image 813
Schneems Avatar asked Sep 17 '25 16:09

Schneems


1 Answers

CC         = gcc
ASAN_FLAGS = -fsanitize=address -fno-omit-frame-pointer -Wno-format-security
ASAN_LIBS  = -static-libasan
CFLAGS    := -Wall -Werror --std=gnu99 -g3
LDFLAGS   += -lpthread

all: hello

hello: tiny_queue.o hello.o
    $(CC) -o $@ $(CFLAGS) $(ASAN_FLAGS) $(CURL_CFLAGS) $^ $(LDFLAGS) $(CURL_LIBS) $(ASAN_LIBS)

You should not specify an Asan library (or a UBsan library, for that matter). Since you are using the compiler driver to drive link, just use -fsanitize=address (this is the recommended way of doing it). Do not add -static-libasan. The compiler driver will add the proper libraries for you.

like image 179
jww Avatar answered Sep 20 '25 06:09

jww