如何使用Makefile创建文件 [英] How to create a file using Makefile

查看:297
本文介绍了如何使用Makefile创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Makefile创建一个.c文件.该C文件的内容如下:

I want to create a .c file from a Makefile. Content of that C file is as follows:

char *variable1 = $(VAR1_FROM_MAKEFILE);
char *variable2 = $(VAR2_FROM_MAKEFILE);

有可能吗?

预先感谢

推荐答案

是的,您可以使用Shell重定向将变量写入源文件:

Yes, you can use shell redirection to write variables in to source file:

VAR1=foobar
all:
    @echo "char *variable1 = \"$(VAR1)\"" > generated.c

(这里的@符号不让make显示echo命令).

(the @ sign is here not to have the echo command displayed by make).

我不知道您的意图是什么,但是将Makefile变量作为宏传递给编译器可能更简单:

I do not know what's your intent here, but it could be simpler to pass the Makefile variables to the compiler as macro :

VAR="toco.conf"
CFLAGS = -DVAR='$(VAR)'
all:
    gcc  $(CFLAGS) prog.c

prog.c为:

#include <stdio.h>

int main(int ac, char **av)
{
    printf("%s\n", VAR);
    exit(0);
}

这篇关于如何使用Makefile创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆