tags:

views:

43

answers:

2

Hi, 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);

Is it possible?

Thanks in advance

A: 

See this question on how you can use sed in a makefile.

kgiannakakis
+1  A: 

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

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

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


EDIT: 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

With prog.c being :

#include <stdio.h>

int main(int ac, char **av)
{
    printf("%s\n", VAR);
    exit(0);
}
philippe
Hi, I am currently passing the variable as a marco to the compiler. But this case fails when the value of VAR changes without modifying the Makefile. e.g VAR := $(shell time +%H%M%S)I want to do this thing is a Makefile for a kernel object. AFAIK There is no "all:" rule in kernel module's Makefile. Otherwise your first solution is great! I know little about Makefiles.