[ORIGINAL CREATION] learning make

本文介绍了一个简单的Makefile示例,用于自动解析C语言源文件并生成描述头文件依赖关系的.d文件。此外,还探讨了C程序中缓冲区管理的一个常见错误,并提供了两种解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

####


$(error error message)
$(warning warning message)

####

The following short makefile can automatically parse the C programming files and generate the .d file describing the dependence on .h files.
####

objects = foo.d bar.d
all : $(objects)
$(objects) : %.d : %.c
    rm -f $@ ; /
    gcc -M $< > $@.$$$$ ; /
    sed 's,[ ]*/($*/)/.o[ :]*,/1/.o $@ : ,g' < $@.$$$$  > $@ ; /
    rm -f $@.$$$$

####
         #include <stdio.h>
         main()
         {
               int c;
               char buf[BUFSIZ];
               setbuf (stdout, buf);
               while ((c = getchar()) != EOF)
                       putchar (c);
         }
      Unfortunately, this program is wrong, for a subtle reason.
      To see where the trouble lies, ask when the buffer is flushed for the last time. Answer: after the main
program has finished, as part of the cleaning up that the library does before handing control back to the
operating system. But by that time, the buffer has already been freed!
There are two ways to prevent this sort of trouble.
First, make the buffer static, either by declaring it explicitly as static:
               static char buf[BUFSIZ];
or by moving the declaration outside the main program entirely.
 Another possibility is to allocate the buffer dynamically and never free it:
               char *malloc();
               setbuf (stdout, malloc (BUFSIZ));
Note that in this latter case, it is unnecessary to check if malloc was successful, because if it fails it will return a null pointer. A null pointer is an acceptable second argument to setbuf; it requests that stdout be unbuffered. This will work slowly, but it will work.

####
foo := a.o b.o c.o
bar := $(foo:.o=.c)
or bar := $(foo:%.o=%.c)

####
SOURCES := $(wildcard /tmp/*.c )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值