-2

I have a C program which I am NOT allowed to edit that looks like this:

#include <stdio.h>

#ifndef YEAR
 #define YEAR "2013"
#endif

int main(){
  printf("Hello world from " YEAR "\n");
  return 0;
}

I need to create a makefile to compile this program and change the YEAR to 2014, so that the output will be "Hello world from 2014" without editing the C program. How can I do this?

greenro
  • 31
  • 6

2 Answers2

4

The compilation command should start with gcc -Wall -DYEAR='"2014"'. It is up to you to code your Makefile with a suitable CFLAGS settings. This answer should be inspirational.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

Just pass a preprocessor directive with the -D parameter, for example:

$ cat tmp.c 
#include <stdio.h>

#ifndef YEAR
 #define YEAR "2013"
#endif

int main(){
  printf("Hello world from " YEAR "\n");
  return 0;
}

$ gcc -DYEAR=\"1234\" tmp.c -o tmp && ./tmp
Hello world from 1234