Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    94

    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 ...

  2. #2
    Just 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.

    Quote Originally Posted by rajkumarmadhani View Post
    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 ...

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    94
    Compilation is in one machine ....
    Actual execution in other machine is it works?

  4. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    645
    Quote Originally Posted by rajkumarmadhani View Post
    Hi,

    I declared variable in Makefile
    like like to know
    Code:
    BUILD_DATE = $(shell date +'%Y%m%d')
    i want to use this variable in c.

    example :test.c
    Code:
    main()
    {
    printf("date:%s\n",BUILD_DATE);
    
    }
    how to do this ...
    Code:
    BUILD_DATE = $(shell date +'%Y%m%d')
    
    .o:.c
         gcc -c -DBUILD_DATE=$(BUILD_DATE)
    Code:
    main()
    {
    printf("date:%s\n",BUILD_DATE);
    
    }

  5. #5
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    224
    There is also the "-D" flag available...

  6. #6
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    94
    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)

  7. #7
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    645
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •