1
votes

I am trying to compile the source code twice with a MACRO defined & undefined. So for default the macro is undefined & i want to define this macro through arguments from Makefile, like

$(MAKE) -c $(present_dir)/new_dir -DONE_MACRO=1

so i am using this ONE_MACRO in file.h. how this definition is reflected and knows to preprocessor. Here im using cygmake. How to pass an MACRO as argument to compiler with cygmake tool.

file.h:-

#if ONE_MACRO
  #define some_targets
#else
 #define other_targets
#endif

So how this ONE_MACRO is defined from make file, im trying to pass macro as argument as below

Makefile:-

MY_TARGET:
    $(MAKE) -C $(present_dir)/target_dir -D ONE_MACRO=1 $(MAKECMDGOALS)
2
Your question is unclear. This shows how to use #ifdef within file.h. Does that answer your question?Beta
It isn't clear what you're asking. The compiler treats the -D MACRO_DEFINE=1 argument (conventionally written without the space between the option letter D and the value MACRO_DEFINE=1) as if there was a line of code that read #define MACRO_DEFINE 1. This is set before any of the actual code is processed. That's straight-forward, so it probably isn't what you're asking — but it isn't clear what you are asking.Jonathan Leffler
I want to pass a macro to compiler from Makefile as an argument, this Macro is using in file.h.so while compiling i'll compile the file.h twice based on the macro as defined & undefined. I think its clear now.Kumar2080

2 Answers

2
votes

Here's an example of how to do what I think you want:

define_test.c:

#include <stdio.h>

#ifndef X
#define X 1
#endif

int
main()
{
    printf("X = %d\n", X);
}

Makefile:

X = 1
CPPFLAGS += -DX=$(X)

define_test: FORCE
        $(CC) $(CPPFLAGS) define_test.c -o $@

FORCE:

Then to pass a different value for macro X:

make X=2

(This assumes you want the value of the macro to be an integer; if you want the value to be a string, you have to get a bit more tricky with quoting properly.)

You can also add the definition directly to the CPPFLAGS, e.g.:

make CPPFLAGS+=-DX=2

2
votes

For every compiler there is an environmental variable, which passes the areguments to compiler/linker. while dealing with other than GCC we need to find that environmental variables before usage.

like wise in my case RVCT31_CCOPT is the variable which passes the arguments to compiler while building.