I have a simple program.

// test.c
#include <stdio.h>


int main()
{
    #ifdef HELLO
    printf("Hello ");
    #endif
    printf("World\n");
    return 0;    
}

I'd like to set up a makefile and either define HELLO or not in that Makefile and then either the program displays "World" or "Hello World" based on whether HELLO is defined. But I don't know where to define HELLO in the Makefile, whether to export it, and how to compile it using gcc. Makefile below.

all: run


test: test.c
	gcc test.c -o test

run: test
	./test

clean:
	rm *~ *.o test

Put it in your gcc invocation:

all: run
 
 
test: test.c
	gcc -D HELLO test.c -o test
 
run: test
	./test
 
clean:
	rm *~ *.o test
commented: :) +15

Yup. That did it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.