Michael
Michael

Reputation:

makefiles CFLAGS

In the process of learning TinyOS I have discovered that I am totally clueless about makefiles.

There are many optional compile time features that can be used by way of declaring preprocessor variables.

To use them you have to do things like:

CFLAGS="-DPACKET_LINK" this enables a certain feature.

and

CFLAGS="-DPACKET_LINK" "-DLOW_POWER" enables two features.

Can someone dissect these lines for me and tell me whats going on? Not in terms of TinyOS, but in terms of makefiles!

Upvotes: 24

Views: 116265

Answers (5)

Isak Savo
Isak Savo

Reputation: 35884

CFLAGS is a variable that is most commonly used to add arguments to the compiler. In this case, it define macros.

So the -DPACKET_LINK is the equivalent of putting #define PACKET_LINK 1 at the top of all .c and .h files in your project. Most likely, you have code inside your project that looks if these macros are defined and does something depending on that:

#ifdef PACKET_LINK
// This code will be ignored if PACKET_LINK is not defined
do_packet_link_stuff();
#endif

#ifdef LOW_POWER
// This code will be ignored if LOW_POWER is not defined    
handle_powersaving_functions();
#endif

If you look further down in your makefile, you should see that $(CFLAGS) is probably used like:

$(CC) $(CFLAGS) ...some-more-arguments...

Upvotes: 29

Michael Burr
Michael Burr

Reputation: 340188

Just for completeness in this - if you're using Microsoft's nmake utility, you might not actually see the $(CFLAGS) macro used in the makefile because nmake has some defaults for things like compiling C/C++ files. Among others, the following are pre-defined in nmake (I'm not sure if GNU Make does anything like this), so you might not see it in a working makefile on Windows:

.c.exe:
    commands:          $(CC) $(CFLAGS) $<

.c.obj:
    commands:          $(CC) $(CFLAGS) /c $<

.cpp.exe:
    commands:          $(CXX) $(CXXFLAGS) $<

.cpp.obj:
    commands:          $(CXX) $(CXXFLAGS) /c $<

Upvotes: 1

Jonas Engstr&#246;m
Jonas Engstr&#246;m

Reputation: 5065

The -D option set pre-processor variables, so in your case, all code that is in the specified "#ifdef / #endif" blocks will be compiled.

I.e.

#ifdef PACKET_LINK
/* whatever code here */
#endif

The CFLAGS is a variable used in the makefile which will be expanded to it's contents when the compiler is invoked.

E.g.

gcc $(CFLAGS) source.c

Upvotes: 3

Flame
Flame

Reputation: 2207

-D stands for define (in gcc) at least, which lets you #define on the command line instead of a file somewhere. A common thing to see would be -DDEBUG or -DNDEBUG which respectively activate or disable debugging code.

Upvotes: 1

Ilya
Ilya

Reputation: 3138

Somewhere in the makefile the CFLAG will be used in compilation line like this:
$(CC) $(CFLAGS) $(C_INCLUDES) $<

and eventually in the execution will be translated to :

gcc -DPACKET_LINK -DLOW_POWER -c filename.c -o filename.o

This define will be passed to the source code as it was define in the header file

Upvotes: 8

Related Questions