在Makefile中进行宏定义-D .

makefile 测试

关键词: Make宏定义 Make传递宏定义 Makefile中添加宏定义 Makefile -D

在Makefile中我们可以通过宏定义来控制源程序的编译。只要在Makefile中的CFLAGS中通过选项-D来指定你于定义的宏即可。

如:
CFLAGS += -D _YUQIANG
在编译的时候加上此选项就可以了: $(CC) $(CFLAGS) $^ -o $@

下面是我写的一个测试文件:

 

例如:

Makefile文件内容为:

CC = gcc
RM = rm

CFLAGS += -D _YUQIANG

TARGETS := myapp

all:$(TARGETS)

$(TARGETS):main.c
$(CC) $(CFLAGS) $^ -o $@

clean:
-$(RM) -f *.o
-$(RM) -f $(TARGETS)


main.c文件的内容为:
#include <stdio.h>

int main()
{

#ifdef _YUQIANG
printf("Hello Yu Qiang, How are you?/n");
#else
printf("Sorry to lost you. /n");
#endif

return 0;
}


make -C src all make[1]: Entering directory '/home/zzuer/Lyp_folder/pmdk/src' make -C libpmem make[2]: Entering directory '/home/zzuer/Lyp_folder/pmdk/src/libpmem' cc -MD -c -o ../nondebug/libpmem/file.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/file.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/file.o ../../src/../src/common/file.c cc -MD -c -o ../nondebug/libpmem/file_posix.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/file_posix.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/file_posix.o ../../src/../src/common/file_posix.c cc -MD -c -o ../nondebug/libpmem/fs_posix.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/fs_posix.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/fs_posix.o ../../src/../src/common/fs_posix.c cc -MD -c -o ../nondebug/libpmem/mmap.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/mmap.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/mmap.o ../../src/../src/common/mmap.c cc -MD -c -o ../nondebug/libpmem/mmap_posix.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/mmap_posix.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/mmap_posix.o ../../src/../src/common/mmap_posix.c cc -MD -c -o ../nondebug/libpmem/os_posix.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/os_posix.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/os_posix.o ../../src/../src/common/os_posix.c cc -MD -c -o ../nondebug/libpmem/os_thread_posix.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/os_thread_posix.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/os_thread_posix.o ../../src/../src/common/os_thread_posix.c cc -MD -c -o ../nondebug/libpmem/os_deep_linux.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/os_deep_linux.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/os_deep_linux.o ../../src/../src/common/os_deep_linux.c cc -MD -c -o ../nondebug/libpmem/os_auto_flush_linux.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/os_auto_flush_linux.c ../../src/../utils/check-os.sh ../../src/../utils/os-banned ../nondebug/libpmem/os_auto_flush_linux.o ../../src/../src/common/os_auto_flush_linux.c cc -MD -c -o ../nondebug/libpmem/out.o -Wno-error -std=gnu99 -I../include -I../common/ -fPIC ../../src/../src/common/out.c ../../src/../src/common/out.c: In function ‘out_init’: ../../src/../src/common/out.c:241:20: error: expected ‘,’ or ‘;’ before ‘SRCVERSION’ 241 | "src version: " SRCVERSION; | ^~~~~~~~~~ make[2]: *** [../Makefile.inc:304: ../nondebug/libpmem/out.o] Error 1 make[2]: Leaving directory '/home/zzuer/Lyp_folder/pmdk/src/libpmem' make[1]: *** [Makefile:167: libpmem] Error 2 make[1]: Leaving directory '/home/zzuer/Lyp_folder/pmdk/src' make: *** [Makefile:83: all] Error 2
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值