www.digitalmars.com         C & C++   DMDScript  

c++ - Eliminating unused data

reply Nicolay Haustov <Nicolay_member pathlink.com> writes:
Are there any options that will cause dmc not to generate string constants when
they are not used? In my project,  I have lots of debugging code,  which is all 
gone for release builds,  but debugging messages are still present in final
executable. I.e. I want "Hello, world." not to be generated from the following
program(simplified).

#ifdef LOGGING
void log(const char *s)
{
printf("%s\n");
}
#else
inline void log(const char *s)
{
}
#endif

int main()
{
log("Hello, world");
return 0;
}
Aug 15 2003
next sibling parent reply "Winfried Mevenkamp" <gemi_gmbh t-online.de> writes:
Try using a macro like this

#define LOGGING 1

#ifdef LOGGING
#define TRACE printf
#else
#define TRACE 1 ? (void)0 : printf
#endif

int main(int argc, char* argv[])
{
   for (int n = 0; n < argc; n++)
      TRACE("Hello, world, argv[%d] = %s\n", n, argv[n]);

   return 0;
}

HTH
Winfried
Aug 15 2003
parent Nicolay Haustov <Nicolay_member pathlink.com> writes:
In article <bhisbv$2o3i$1 digitaldaemon.com>, Winfried Mevenkamp says...

Try using a macro like this
Thanks for the response. As always, real world case is more complex. I need to log different data, including my own classes. I am using C++ operator <<, something like this, again simplified. Using macro here would mean uglifying code, but looks like I will have to do it... class logger {}; class A {}; #ifdef LOGGING logger& operator<<(logger& log, const char *s) { printf("%s\n", s); return log; } logger& operator<<(logger& log, const A& a) { printf("A\n"); return log; } #else inline logger& operator<<(logger& log, const char *s) { return log; } inline logger& operator<<(logger& log, const A& a) { return log; } #endif int main() { logger log; log << "Hello, world!" << A(); return 0; }
Aug 18 2003
prev sibling parent reply Ilya Minkov <midiclub 8ung.at> writes:
You may want to look up function-level linkage in the manual.

Nicolay Haustov wrote:
 Are there any options that will cause dmc not to generate string constants when
 they are not used? In my project,  I have lots of debugging code,  which is
all 
 gone for release builds,  but debugging messages are still present in final
 executable. I.e. I want "Hello, world." not to be generated from the following
 program(simplified).
 
 #ifdef LOGGING
 void log(const char *s)
 {
 printf("%s\n");
 }
 #else
 inline void log(const char *s)
 {
 }
 #endif
 
 int main()
 {
 log("Hello, world");
 return 0;
 }
 
 
Aug 15 2003
parent Nicolay Haustov <Nicolay_member pathlink.com> writes:
In article <bhj0tt$2s1a$2 digitaldaemon.com>, Ilya Minkov says...

You may want to look up function-level linkage in the manual.
Thanks for response. I had a feeling that this option(-Nc) only helps eliminate unused functions. This is not a problem: all debugging code is gone from final executable. However, all debug messages are still present. And indeed, it seems to have no effect(just tried).
Aug 18 2003