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

5
  • 1
    It is in the manual -- linux.die.net/man/1/gcc Commented Nov 2, 2014 at 20:05
  • Look at your compiler's -D option. Commented Nov 2, 2014 at 20:05
  • 1
    can the OP please post the University and the class number that this assignment is from? I am just curious. Commented Nov 2, 2014 at 21:28
  • It's from Polotehnica University of Bucharest. Commented Nov 3, 2014 at 4:46
  • Thanks for the answers. I'm seeing that my question has been answered before. Sorry about that. Commented Nov 3, 2014 at 4:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

It is C code (not C++), so tmp.c as a source file (not tmp.cpp) and gcc as a compiler (not g++).
Basile Starynkevitch, you're right. Updated the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.