473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

optional debugging


Hello

I hope some one maybe point me to targeted reading for the issue at
hand, or explain how/why to fix this. thanks

in the code below, I need to conditionally use P(A) to allow debugging
code to be automatically stripped out by setting a command-line flag.

I did quite a bit of readying but confused as to how to implement what
I learned, CXXFLAGS which be default has the preprocessor options,
ifdef else endif usage...

******************************code**************** **************
#include <iostream>
using namespace std;

#ifdef DEBUG
#define P(A) cout << #A << ": " << (A) << endl;
#endif

int main() {
int a = 1, b = 2, c = 3;

//debugging starts
P(a); P(b); P(c);
P(a + b);
P((c - a)/b);
//debugging ends

} ///:~

******************************makefile************ ******************
CPP= g++
proj1: main.o
$(CPP) main.o -o proj1

#main.o: main.cpp
..SUFFIXES: .o .cpp
..cpp.o:
$(CPP) -c $<
Aug 10 '05 #1
4 1372
Baloff wrote:
Hello

I hope some one maybe point me to targeted reading for the issue at
hand, or explain how/why to fix this. thanks

in the code below, I need to conditionally use P(A) to allow debugging
code to be automatically stripped out by setting a command-line flag.

I did quite a bit of readying but confused as to how to implement what
I learned, CXXFLAGS which be default has the preprocessor options,
ifdef else endif usage...

******************************code**************** **************
#include <iostream>
using namespace std;

#ifdef DEBUG
#define P(A) cout << #A << ": " << (A) << endl; #else
#define P(A) ((void)0) #endif

int main() {
int a = 1, b = 2, c = 3;

//debugging starts
P(a); P(b); P(c);
P(a + b);
P((c - a)/b);
//debugging ends

} ///:~

******************************makefile************ ******************
CPP= g++ CPP=g++ -DDEBUG proj1: main.o
$(CPP) main.o -o proj1

#main.o: main.cpp
.SUFFIXES: .o .cpp
.cpp.o:
$(CPP) -c $<

Aug 10 '05 #2
red floyd <no*****@here.dude> writes:
Baloff wrote:
Hello
I hope some one maybe point me to targeted reading for the issue at
hand, or explain how/why to fix this. thanks in the code below, I
need to conditionally use P(A) to allow debugging
code to be automatically stripped out by setting a command-line flag.
I did quite a bit of readying but confused as to how to implement
what
I learned, CXXFLAGS which be default has the preprocessor options,
ifdef else endif usage...
******************************code**************** **************
#include <iostream>
using namespace std;
#ifdef DEBUG
#define P(A) cout << #A << ": " << (A) << endl;

#else
#define P(A) ((void)0)
#endif
int main() {
int a = 1, b = 2, c = 3;
//debugging starts
P(a); P(b); P(c);
P(a + b);
P((c - a)/b);
//debugging ends
} ///:~
******************************makefile************ ******************
CPP= g++

CPP=g++ -DDEBUG
proj1: main.o
$(CPP) main.o -o proj1
#main.o: main.cpp
.SUFFIXES: .o .cpp
.cpp.o:
$(CPP) -c $<


who do you modify the makefile? to be able to issue $make debug
Aug 11 '05 #3
Hello,

Baloff wrote:

#ifdef DEBUG
#define P(A) cout << #A << ": " << (A) << endl;
#endif
And what shall happen if DEBUG is not defined, if the macro P is not
defined, you will get syntax errors. Therefore:

#ifdef DEBUG
#define P(A) cout << #A << ": " << (A) << endl;
#else
#define P(A)
#endif

******************************makefile************ ******************
CPP= g++
proj1: main.o
$(CPP) main.o -o proj1

#main.o: main.cpp
.SUFFIXES: .o .cpp

..cpp.o:
$(CPP) -DDEBUG -c $<
# comment: no debug: $(CPP) -c $<

You violate a lot of conventions. Usually (at least in GNU make) there
is the variable CXX for the C++ compiler, and CXXFLAGS for flags for
the C++ compiler. If you use the builtin rule of make for C++, i.e. .cc
to .o, you may just write

proj1: main.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^

CXXFLAGS=-DDEBUG

Read the make manual about predefined rules and variables. There is a
predefined rule for compiling single .cc files to .o files. And there
is a predefined rule for compiling and linking a single .cc file to an
executable, i.e. proj1.cc to executable proj1. Everything else you have
to provide yourself in the makefile. And take care to not break
existing conventions, or nobody will be able to understand your
makefiles. The variable CPP is used for the preprocessor executable.

There are libraries out supporting logging, so search the Web to save
yourself from reinventing the wheel. Or do you do this for educational
purposes?

Bernd Strieder


Aug 11 '05 #4
Bernd Strieder <st******@informatik.uni-kl.de> writes:
Hello,

Baloff wrote:

#ifdef DEBUG
#define P(A) cout << #A << ": " << (A) << endl;
#endif
And what shall happen if DEBUG is not defined, if the macro P is not
defined, you will get syntax errors. Therefore:

#ifdef DEBUG
#define P(A) cout << #A << ": " << (A) << endl;
#else
#define P(A)
#endif

******************************makefile************ ******************
CPP= g++
proj1: main.o
$(CPP) main.o -o proj1

#main.o: main.cpp
.SUFFIXES: .o .cpp

.cpp.o:
$(CPP) -DDEBUG -c $<
# comment: no debug: $(CPP) -c $<

You violate a lot of conventions. Usually (at least in GNU make) there
is the variable CXX for the C++ compiler, and CXXFLAGS for flags for
the C++ compiler. If you use the builtin rule of make for C++, i.e. .cc
to .o, you may just write

proj1: main.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^

CXXFLAGS=-DDEBUG

Read the make manual about predefined rules and variables. There is a
predefined rule for compiling single .cc files to .o files. And there
is a predefined rule for compiling and linking a single .cc file to an
executable, i.e. proj1.cc to executable proj1. Everything else you have
to provide yourself in the makefile. And take care to not break
existing conventions, or nobody will be able to understand your
makefiles. The variable CPP is used for the preprocessor executable.

There are libraries out supporting logging, so search the Web to save
yourself from reinventing the wheel. Or do you do this for educational
purposes?

I am going through the book "Thinking in C++" and following each
chapter and doing the exercises. but I will do my best to understand
instructions given on the newsgroup.


Bernd Strieder

Aug 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: bearophileHUGS | last post by:
Adding Optional Static Typing to Python looks like a quite complex thing, but useful too: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 I have just a couple of notes: Boo...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
12
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
1
by: Tookelso | last post by:
Hello, I would like to have a group of elements which are *required* in one context, but each one is *optional* in another context. For example: I have a configuration file which has a...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
2
by: Nathan Harmston | last post by:
Hi, I know this isnt the pyparsing list, but it doesnt seem like there is one. I m trying to use pyparsing to parse a file however I cant get the Optional keyword to work. My file generally...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.