2017-06-12 92 views
0

我想寫一個makefile,我可以在linux和mac上使用地址消毒劑構建。這適用於我的無業遊民例如:Makefile爲Linux和Mac與地址消毒劑

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 [email protected] $(CFLAGS) $(ASAN_FLAGS) $(CURL_CFLAGS) $^ $(LDFLAGS) $(CURL_LIBS) $(ASAN_LIBS) 

這部作品ubuntu/trusty64,但無法在我的Mac與

$ 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 

有誰知道怎麼寫了Mac和Linux兼容的makefile?

p.s.我對C很陌生,對不起,如果這個問題是超級基礎。

+0

爲編譯器和鏈接器構建具有'-pthread'標誌的多線程代碼。 '-lpthread'不夠。 –

+0

請參閱[AddressSanitizer](http://clang.llvm.org/docs/AddressSanitizer.html) –

+0

這與C語言無關。 (嚴格地說,甚至不是makefile問題)。 – Olaf

回答

1
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 [email protected] $(CFLAGS) $(ASAN_FLAGS) $(CURL_CFLAGS) $^ $(LDFLAGS) $(CURL_LIBS) $(ASAN_LIBS) 

你應該指定峨山庫(或UBsan庫,爲此事)。由於您使用編譯器驅動程序來驅動鏈接,只需使用-fsanitize=address(這是推薦的方式)。做不是-static-libasan。編譯器驅動程序將爲您添加適當的庫。

+0

謝謝,這個伎倆。我刪除了'ASAN_LIBS'部分,因爲'-fsanatize = address'已經在'ASAN_FLAGS'中。 – Schneems