Results 1 to 7 of 7
Hi,
I declared variable in Makefile
like
BUILD_DATE = $(shell date +'%Y%m%d')
i want to use this variable in c.
example :test.c
main()
{
printf("date:%s\n",BUILD_DATE);
}
how to do this ...
- 08-08-2011 #1Just Joined!
- Join Date
- Nov 2006
- Location
- Hyderabad
- Posts
- 85
call variable from Makefile
Hi,
I declared variable in Makefile
like
BUILD_DATE = $(shell date +'%Y%m%d')
i want to use this variable in c.
example :test.c
main()
{
printf("date:%s\n",BUILD_DATE);
}
how to do this ...
- 08-09-2011 #2Just Joined!
- Join Date
- Apr 2011
- Location
- India
- Posts
- 2
Hey,
You can do an export for the variable which u have defined in the Makefile using the below command,
"export BUILD_DATE"
Now the variable defined in the makefile will become an environment variable and it will be available for access by the other programs.
In the c program you can use the API getenv(BUILD_DATE) to get the value of the variable BUILD_DATE.
FYI, I have not checked it.
- 08-09-2011 #3Just Joined!
- Join Date
- Nov 2006
- Location
- Hyderabad
- Posts
- 85
Compilation is in one machine ....
Actual execution in other machine is it works?
- 08-09-2011 #4Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
- 08-09-2011 #5Just Joined!
- Join Date
- Nov 2009
- Posts
- 53
There is also the "-D" flag available...
- 08-09-2011 #6Just Joined!
- Join Date
- Nov 2006
- Location
- Hyderabad
- Posts
- 85
BUILD_DATE = $(shell date +'%Y%m%d')
BUILD_NUM = $(shell cat $(PROG_BUILD_NUM))
CFLAGS = -DBUILD_DATE=$(BUILD_DATE) -DBUILD_NUM=$(BUILD_NUM)
app:$(OBJS)
$(CC) $(CFLAGS) -o app $(OBJS)
still iam getting error like below.
test.c:35: error: 'BUILD_DATE' undeclared (first use in this function)
test.c:35: error: (Each undeclared identifier is reported only once
test.c:35: error: for each function it appears in.)
test:36: error: 'BUILD_NUM' undeclared (first use in this function)
- 08-11-2011 #7Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
The problem is that the "BUILD_DATE" passed in is not a string and it needs to be converted into a string to be printed.
Sample file "code.c"
Code:#include <stdlib.h> #include <stdio.h> #include <stdlib.h> #define Str(arg) #arg #define StrValue(arg) Str(arg) #define STR_BUILD_DATE StrValue(BUILD_DATE) char build_date[] = STR_BUILD_DATE; int main() { fprintf(stdout,"%s\n", build_date); return 0; }Code:gcc -o code -DBUILD_DATE=20110811 code.c ./code gcc -o code -DBUILD_DATE=20101102 code.c ./code


Reply With Quote
