GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Jul-2008, 13:38
emp emp is offline
Junior Member
 
Join Date: Nov 2007
Posts: 35
emp is an unknown quantity at this point

Help with -D flag in make file


I am using GNU compiler on redhat linux.

I used the -D flag in my makefile to set a #define.

it is used -DDEBUG_FLAG etc etc

In my code where I specify

#ifdef DEBUG_FLAG
do debug printing
#else
print "debug _flag is off"
#endif

If the -DDEBUG_FLAG is in the make file the code compiles and runs as expected.
When I remove the -DDEBUG_FLAG I get a compiler error that the variable DEBUG_FLAG is unidentified. at the #ifdef statement.

I want a global flag set and thought this was the easiest way to implement it. Any suggestions to get what I have implemented to work??

Or alternative suggestions.

Thanks

EMP
  #2  
Old 07-Jul-2008, 21:55
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Help with -D flag in make file


Please post your code and complete make file to here with code tags surrounded.
  #3  
Old 07-Jul-2008, 22:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with -D flag in make file


Quote:
Originally Posted by emp
I am using GNU compiler on redhat linux...I used the -D flag in my makefile to set a #define...


Sounds like a reasonable thing to try. Here's how I might do it. (There are, undoubtedly, other ways.)


CPP / C++ / C Code:
/* testflag.c
 *
 * Reports whether something was defined at
 * compile time
 *
 */

#include <stdio.h>

int main()
{

#ifdef DEBUG_FLAG
    printf("DEBUG_FLAG is defined.\n");
#else
    printf("DEBUG_FLAG is not defined.\n");
#endif

    return 0;
}

Here's the Makefile:
Code:
CC = gcc CFLAGS = -Wall -W -pedantic FLAG = -DDEBUG_FLAG CFLAGS += $(FLAG) TARGET = testflag $(TARGET): $(TARGET).c @echo "In Makefile: FLAG = <$(FLAG)>" $(CC) $(CFLAGS) $< -o $@ clean: rm -f $(TARGET)

Here's what happens when I make it and execute
Code:
$ make clean; make && ./testflag rm -f testflag In Makefile: FLAG = <-DDEBUG_FLAG> gcc -Wall -W -pedantic -DDEBUG_FLAG testflag.c -o testflag DEBUG_FLAG is defined.

Now comment out the FLAG line in the Makefile:
Code:
#FLAG = -DDEBUG_FLAG

Then
Code:
$ make clean; make && ./testflag rm -f testflag In Makefile: FLAG = <> gcc -Wall -W -pedantic testflag.c -o testflag DEBUG_FLAG is not defined.

Now, leave it commented out and change the command line to make:

Code:
$ make clean; make FLAG="-DDEBUG_FLAG" && ./testflag rm -f testflag In Makefile: FLAG = <-DDEBUG_FLAG> gcc -Wall -W -pedantic -DDEBUG_FLAG testflag.c -o testflag DEBUG_FLAG is defined.

Regards,

Dave
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 04:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.