0

Below is my c code:

#include

main() {    
    #ifdef HELLO
        printf(" hello ");
    #else
        printf(" else ");
    #endif
}

And my Makefiles is:

all:
        gcc uss_main.c -o out

Here I am trying to print hello by executing the binary out. I want to define the macro HELLO only at the time of doing make. I have tried using make HELLO=11but it only printing the else part, I am pretty new to make. Please help me to enhance my Makefile. Thanks in advance.

1

You can define macro by using GCC compiler flag like this:

gcc -DHELLO=1 uss_main.c -o out

If you want to pass it to make, you need to modify Makefile. Something like this:

USE_HELLO = $(if $(HELLO),-DHELLO=$(HELLO),)

all:
     gcc $(USE_HELLO) uss_main.c -o out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.