473,395 Members | 1,474 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.

Disable printf

I am working on a Memory Footprint Reduction project. I came across an
idea to disable all printf statements so that less memory is required.
In addition, when there is no single printf statement, the printf
library will not be linked, so it further reduces the executable size.
Is there an easy way to disable printf in a large project? In my
project, we have thousands of C & C++ files. They don't have a common
included common header. Otherwise, I can do a #define printf(x) in the
common header. I am using GNU GCC compiler and run the program in both
Linux and VxWorks platform.

Any of you have a suggestion on disabling all printf statements?

I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?

Dec 1 '06 #1
19 9810

RedDevilDan wrote:
I am working on a Memory Footprint Reduction project. I came across an
idea to disable all printf statements so that less memory is required.
In addition, when there is no single printf statement, the printf
library will not be linked, so it further reduces the executable size.
Is there an easy way to disable printf in a large project? In my
project, we have thousands of C & C++ files. They don't have a common
included common header. Otherwise, I can do a #define printf(x) in the
common header. I am using GNU GCC compiler and run the program in both
Linux and VxWorks platform.

Any of you have a suggestion on disabling all printf statements?

I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?

This is of course about tool sets, not about the C language.

<OT>

Some random ideas.

You might be able to use gcc's -D'name(args..)=definition' option.
Depending on how things are arranged you might be able to
alias gcc or you may have to play around a blit with "PATH" and
the names of binary executables.

You could use your favourite scripting language to
insert the line #include "common.h" at the start of any file
ending ".c" or "cpp". Or you could compile generic.c by compiling
a file containing

#include "common.h"
#include "generic.c"

Again, you should be able to wrap this in a script, call it gcc, change
the PATH variable so that your gcc is called first and link
actual_gcc to /usr/bin/gcc and call actual_gcc from within the srcipt.
Then when the makefiles call gcc, they will get your version

</OT>
- William Hughes

Dec 1 '06 #2
Thanks for the reply at first.

I tried the gcc -D "printf(x)=" way. However, it complains about the
printf(x) at the stdio.h, "/usr/include/stdio.h:329:58: macro "printf"
passed 2 arguments, but takes just 1"
I guess the gcc doesn't know how to handle the std c lib if you
redefine printf

I like the idea of putting #include "common.h" line into each c or cpp
files. Do you know how? What kind of script can do this?

Thanks
William Hughes wrote:
RedDevilDan wrote:
I am working on a Memory Footprint Reduction project. I came across an
idea to disable all printf statements so that less memory is required.
In addition, when there is no single printf statement, the printf
library will not be linked, so it further reduces the executable size.
Is there an easy way to disable printf in a large project? In my
project, we have thousands of C & C++ files. They don't have a common
included common header. Otherwise, I can do a #define printf(x) in the
common header. I am using GNU GCC compiler and run the program in both
Linux and VxWorks platform.

Any of you have a suggestion on disabling all printf statements?

I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?


This is of course about tool sets, not about the C language.

<OT>

Some random ideas.

You might be able to use gcc's -D'name(args..)=definition' option.
Depending on how things are arranged you might be able to
alias gcc or you may have to play around a blit with "PATH" and
the names of binary executables.

You could use your favourite scripting language to
insert the line #include "common.h" at the start of any file
ending ".c" or "cpp". Or you could compile generic.c by compiling
a file containing

#include "common.h"
#include "generic.c"

Again, you should be able to wrap this in a script, call it gcc, change
the PATH variable so that your gcc is called first and link
actual_gcc to /usr/bin/gcc and call actual_gcc from within the srcipt.
Then when the makefiles call gcc, they will get your version

</OT>
- William Hughes
Dec 1 '06 #3
"RedDevilDan" <kl******@yahoo.comwrites:
Thanks for the reply at first.

I tried the gcc -D "printf(x)=" way. However, it complains about the
printf(x) at the stdio.h, "/usr/include/stdio.h:329:58: macro "printf"
passed 2 arguments, but takes just 1"
I guess the gcc doesn't know how to handle the std c lib if you
redefine printf

I like the idea of putting #include "common.h" line into each c or cpp
files. Do you know how? What kind of script can do this?
Please don't top-post. Read the following:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

I'm sure you can use some kind of script to add #include "common.h" to
each of your source files, but that's a question for a different
newsgroup. If you're on a Unix-like system, try comp.unix.shell or
comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 1 '06 #4
RedDevilDan wrote:
I am working on a Memory Footprint Reduction project. I came across an
idea to disable all printf statements so that less memory is required.
In addition, when there is no single printf statement, the printf
library will not be linked, so it further reduces the executable size.
Is there an easy way to disable printf in a large project? In my
project, we have thousands of C & C++ files. They don't have a common
included common header. Otherwise, I can do a #define printf(x) in the
common header. I am using GNU GCC compiler and run the program in both
Linux and VxWorks platform.

Any of you have a suggestion on disabling all printf statements?

I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?
See William Hughes' reply for some ideas about introducing
a #define to "spike" printf. You might also want to spike the
related functions fprintf, sprintf, vprintf, vfprintf, vsprintf,
and possibly the scanf family as well.

But since you have "thousands of C & C++ files," it seems
doubtful that eliminating these parts of the library will make
a meaningful difference. It might, of course, and there are
always those cases where shrinking the program by twenty-seven
bytes means you don't need to buy another megabyte of memory.
But in all those "thousands" of other files, I sort of suspect
that you're likely to find other and better opportunities to
save. It's a fine thing to clear the bottle caps off the beach,
but doing something about the whale carcases comes first.

Another thing to note is that on many platforms, eliminating
references to a library function will shrink the program by a
grand total of zero bytes. Some systems (and I believe Linux is
among them) attach the entire Standard library to the program as
one indivisible unit, even if you use only strlen() and exit().
On these systems you'll get the whole library or none, and there
are no half-measures. (Well, maybe one half-measure: Some systems
divide the Standard library into a "core" portion and a "math"
portion, and if you never use sqrt() and sin() and so on you may
escape the burden of including the "math" part. But it's very
likely that you'll be unable to pick and choose: If you use so
much as malloc(), you'll probably get printf() anyhow, even if
you didn't ask for it.)

One space-saving suggestion: Try different optimization
levels on the compiler, if it offers them. Minimally optimized
code tends to be largish (it includes a lot of extraneous loads
and stores), and extravagantly optimized code also tends to be
largish (it unrolls loops and in-lines function bodies). You may
find that there's an in-between point that minimizes code size --
of course, you'll also need to balance this against execution
speed.

--
Eric Sosman
es*****@acm-dot-org.invalid

Dec 1 '06 #5

RedDevilDan wrote:
Thanks for the reply at first.

I tried the gcc -D "printf(x)=" way. However, it complains about the
printf(x) at the stdio.h, "/usr/include/stdio.h:329:58: macro "printf"
passed 2 arguments, but takes just 1"
I guess the gcc doesn't know how to handle the std c lib if you
redefine printf
Probably no. More likely the problem is that printf is a variadic
function
(takes a varying number of arguments) so you need to
replace it a variadic macro. Such macros do not exist
is C90, but they do exists in C99 and as a gcc extension. You
need something like

-D "printf(x,...)="
>
I like the idea of putting #include "common.h" line into each c or cpp
files. Do you know how? What kind of script can do this?
Just about any. If you don't have a favourite scripting language,
then this is as good a time as any to learn one. I would do this
sort of thing in Python (much more than "just" a scripting
language, but a good choice nonetheless), but there are many
alternatives
(for Unix) bash and csh shells scripts, Perl, ... Details are
very off topic here.

For details on aliasing and/or playing games with paths and filenames
try asking in comp.unix.programmer

But, as Eric Sosman pointed out, getting rid of printf might have
little to do with solving your problem. (Have you tried passing
-Os to gcc? ) Before you spend much effort on this at least
check a simple example to see if removing printf makes enough
of a difference to be worth doing.

- William Hughes

Dec 1 '06 #6
MQ

Eric Sosman wrote:
>
Another thing to note is that on many platforms, eliminating
references to a library function will shrink the program by a
grand total of zero bytes.
AFAIK, most applications on Linux load the C library as a shared
dynamic library, which is mapped into the processes application space
when the application requires it. In other words, the C library is
loaded once and is shared by all applications. Not only would you have
to remove the references to the C library from your application, but
ensure that every application running on the system does not use the C
library. If just one application uses it, then the benefit of doing
this is non-existent.

MQ

Dec 1 '06 #7
On 30 Nov 2006 17:58:34 -0800, "RedDevilDan" <kl******@yahoo.com>
wrote:
>I am working on a Memory Footprint Reduction project. I came across an
idea to disable all printf statements so that less memory is required.
In addition, when there is no single printf statement, the printf
library will not be linked, so it further reduces the executable size.
Is there an easy way to disable printf in a large project? In my
project, we have thousands of C & C++ files. They don't have a common
included common header. Otherwise, I can do a #define printf(x) in the
common header. I am using GNU GCC compiler and run the program in both
Linux and VxWorks platform.
Rule:

Don't use printf if you suspect it will ever contribute to a "Memory
Footprint" problem.

I hope you're not undertaking this project because someone violated
this rule.
>
Any of you have a suggestion on disabling all printf statements?

I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?
The Microsoft Visual C++ compiler provides the /FI compiler option
(accurately labeled in the superlative help system as the "Name Forced
Include File" option):

"The /FIfilename option causes the preprocessor to process the header
file specified by filename. Each filename is included as if it were
specified with double quotation marks (") in an #include directive on
line 0 of every C or C++ source file specified in the CL environment
variable, on the command line, or in any command file. If multiple /FI
options are used, the files are included in the order they are
processed by CL. The space between /FI and filename is optional."

This is one of my favorite options. I use it to "compile" code, using
VC++, which is targeted for other compilers and platforms, such as
gcc/QNX/x86/PPC, Watcom/QNX, CVI/Windows, HI-TECH/PIC,
CodeComposer/TI-DSP, gcc/Atmel, etc. I can develop all of my code,
regardless of compiler/platform, in a single development environment,
thanks to Microsoft and the /FI option.

I don't know if gcc has a similar option. If not, you can always just
get the source code to gcc and modify it to implement the equivalent
of the /FI option. That's one of the beauty's of open source software,
isn't it?.

Best regards
--
jay

Dec 1 '06 #8
"RedDevilDan" <kl******@yahoo.comwrites:
I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?
Are you telling me you can't write a program to add one line of
text to each file in a collection of them?
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Dec 1 '06 #9
On Thu, 30 Nov 2006 23:16:51 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
>"RedDevilDan" <kl******@yahoo.comwrites:
>I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?

Are you telling me you can't write a program to add one line of
text to each file in a collection of them?
He may be telling you something like he wrote such a program but
because he forgot to check out the files from source control they were
read-only and his program failed to modify the files. And after hours
and hours of debugging his program, he finally got it working after
checking out the files from source control, only to suffer the wrath
of the Change Review Board (CRB) when they asked why so many source
files were modified. The source code modifications were straight
forward and trivial, but a CRB that only looks at statistics never
considered that. (Some CRBs are like a judge, and ironically, some
Federal Court Judges here in the US are like some CRBs).

If the OP had something like the Microsoft VC++ /FI option, as
described else-thread, he would have intuitively modified a Makefile
file or project file and added a header file and have been done with
it. That's adept, adroit, agile, astute, and, ultimately, awesome.

--
jay
Dec 1 '06 #10
In article <11**********************@j44g2000cwa.googlegroups .com>,
RedDevilDan <kl******@yahoo.comwrote:
>I am working on a Memory Footprint Reduction project. I came across an
idea to disable all printf statements so that less memory is required.
If you can add a header to each file, and you are using GCC, you can
put this in it (inside an ifdef that checks for GCC):

extern int printf(const char *format, ...) __attribute__ ((deprecated));

which will result in the message

warning: 'printf' is deprecated

when you compile a file that calls printf.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 1 '06 #11
jaysome <ja*****@spamcop.netwrites:
On 30 Nov 2006 17:58:34 -0800, "RedDevilDan" <kl******@yahoo.com>
wrote:
>>I am working on a Memory Footprint Reduction project.
<snip>
>>They don't have a common
included common header. Otherwise, I can do a #define printf(x) in the
common header. I am using GNU GCC compiler and run the program in both
Linux and VxWorks platform.
<snip>
The Microsoft Visual C++ compiler provides the /FI compiler option
<snip>
I don't know if gcc has a similar option.
Now OT, but yes, it has -include <fileand the even more useful
-imacros <fileoption.

--
Ben.
Dec 1 '06 #12
jaysome <ja*****@spamcop.netwrites:
If the OP had something like the Microsoft VC++ /FI option, as
described else-thread, he would have intuitively modified a Makefile
file or project file and added a header file and have been done with
it. That's adept, adroit, agile, astute, and, ultimately, awesome.
....and can be rather a surprise when something goes wrong,
because there's nothing in the source code indicating that the
header file gets included.
--
"...deficient support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup
Dec 1 '06 #13

"RedDevilDan" <kl******@yahoo.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>I am working on a Memory Footprint Reduction project. I came across an
idea to disable all printf statements so that less memory is required.
In addition, when there is no single printf statement, the printf
library will not be linked, so it further reduces the executable size.
Is there an easy way to disable printf in a large project? In my
project, we have thousands of C & C++ files. They don't have a common
included common header. Otherwise, I can do a #define printf(x) in the
common header. I am using GNU GCC compiler and run the program in both
Linux and VxWorks platform.

Any of you have a suggestion on disabling all printf statements?

I also think about to add a common header file into the whole project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to insert
the line automatically into each c file?
Using a macro to disable printf will not work in the general case.
Suppose you had this:
#define printf(x)
or even using the variadic C99
#define printf(x,...)

But printf is a function that returns an int. Think what will happens if the
code contains:
if ( printf(....) != n ) {...}

One way around this is to add your own printf function that does nothing:

int printf( char *fmt, ... ) {}

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Dec 1 '06 #14
Fred Kleinschmidt said:
>
"RedDevilDan" <kl******@yahoo.comwrote in message
<snip>
>>
Any of you have a suggestion on disabling all printf statements?
<snip>
>
One way around this is to add your own printf function that does nothing:

int printf( char *fmt, ... ) {}
4.1.2 of C89 says: "All external identifiers declared in any of the headers
are reserved, whether or not the associated header is included."

7.1.3 of C99 says: "- Each identifier with file scope listed in any of the
following subclauses (including the future library directions) is reserved
for use as a macro name and as an identifier with file scope in the same
name space if any of its associated headers is included."

So - if you have a C99 compiler, that's fine **provided** that you don't
need anything else from <stdio.h>! But if you don't have a C99 compiler, it
ain't at all fine.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #15
#define printf( f,...) (1)

// or zero, printf can be used in an expression and needs to be an
expression result.
// that's it.

// provided you have a c99 compiler. GCC, OpenWatcom,...

RedDevilDan:
I am working on a Memory Footprint Reduction project. I came across
an
idea to disable all printf statements so that less memory is
required.
In addition, when there is no single printf statement, the printf
library will not be linked, so it further reduces the executable
size.
Is there an easy way to disable printf in a large project? In my
project, we have thousands of C & C++ files. They don't have a
common
included common header. Otherwise, I can do a #define printf(x) in
the
common header. I am using GNU GCC compiler and run the program in
both
Linux and VxWorks platform.

Any of you have a suggestion on disabling all printf statements?

I also think about to add a common header file into the whole
project.
However, I would need to put a #include "common.h" line into each c
file. It's gonna take a while to do that. Is there any way to
insert
the line automatically into each c file?
Dec 1 '06 #16
Richard Heathfield wrote:
Fred Kleinschmidt said:

"RedDevilDan" <kl******@yahoo.comwrote in message

<snip>
>
Any of you have a suggestion on disabling all printf statements?
<snip>

One way around this is to add your own printf function that does nothing:

int printf( char *fmt, ... ) {}

4.1.2 of C89 says: "All external identifiers declared in any of the headers
are reserved, whether or not the associated header is included."

7.1.3 of C99 says: "- Each identifier with file scope listed in any of the
following subclauses (including the future library directions) is reserved
for use as a macro name and as an identifier with file scope in the same
name space if any of its associated headers is included."

So - if you have a C99 compiler, that's fine **provided** that you don't
need anything else from <stdio.h>! But if you don't have a C99 compiler, it
ain't at all fine.
7.1.3 of C99 also says "All identifiers with external linkage in any
of the following subclauses (including the future library directions)
are always reserved for use as identifiers with external linkage."

It's not fine in C99, even if you don't use anything from <stdio.h>.

Dec 1 '06 #17
Harald van Dijk said:

<snip>
>
7.1.3 of C99 also says "All identifiers with external linkage in any
of the following subclauses (including the future library directions)
are always reserved for use as identifiers with external linkage."

It's not fine in C99, even if you don't use anything from <stdio.h>.
Good spot. Even without that, it's impractical (because just about every
program needs *something* from stdio.h), but with it, it's a wipe-out.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #18

Richard Heathfield wrote:
Harald van Dijk said:

<snip>

7.1.3 of C99 also says "All identifiers with external linkage in any
of the following subclauses (including the future library directions)
are always reserved for use as identifiers with external linkage."

It's not fine in C99, even if you don't use anything from <stdio.h>.

Good spot. Even without that, it's impractical (because just about every
program needs *something* from stdio.h), but with it, it's a wipe-out.

--
Richard Heathfield
I wonder why no one has suggested the real solution. Replace the
library.
But I guess that solution is really outside the realm of C.

Ed

Dec 1 '06 #19

On Fri, 30 Nov 2006, William Hughes wrote:
RedDevilDan wrote:
>Thanks for the reply at first.

I tried the gcc -D "printf(x)=" way. However, it complains about the
printf(x) at the stdio.h, "/usr/include/stdio.h:329:58: macro "printf"
passed 2 arguments, but takes just 1"
I guess the gcc doesn't know how to handle the std c lib if you
redefine printf

Probably no. More likely the problem is that printf is a variadic
function (takes a varying number of arguments) so you need to
replace it a variadic macro.
Probably, the problem is that that line in stdio.h is

extern int printf(const char *fmt, ...);

which --- even with a variadic macro --- will become something like

extern int ((void)0);

which is just plain wrong. The most correct answers, already provided
by other posters, are "don't do that", "get a new library (without
printf)", and "get a new compiler (which can optimize out dead code)".

-Arthur,
intentionally conflating the concepts of "compiler" and "linker"
Dec 2 '06 #20

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

Similar topics

5
by: Bob Bedford | last post by:
I create checkboxes using datas from a database (with PHP). I store all values in an array, as I must pass this value like a Form value. Now, in some cases, when a checkbox is selected, I must...
6
by: nntp | last post by:
I have a set of links which I want search engines to crawl them, but I want to disable them from my visitors, so I will ask the link owners to pay me to let me enable them. <a disabled...
12
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
13
by: Rich | last post by:
Hello, So I would like to disable a textbox on a vb.net form without the text getting grayed out. In vb6 I could place a textbox control on a frame control and disable the frame leaving the...
4
by: Chris | last post by:
I have an asp.net page say page1.aspx. The form in html code is <form id = "Form1"> And i want to disable all the fields of the form after some code steps. I had created a javascript funct: ...
0
by: Ahmad Jalil Qarshi | last post by:
Hi! I have a problem while developing some webpages.The Problem is that:- How We Can Disable The Controls Of One Web Form From Other Web Form In Asp.net? Explanation:- There Should Be Two...
4
by: Phoe6 | last post by:
Hi all, I am trying to disable the NIC card (and other cards) enabled in my machine to test diagnostics on that card. I am trying to disable it programmatic using python. I checked python wmi and...
0
by: sainiranji | last post by:
Hi All I have diffrent categories in diffrrent logging purpose and all are working fine...but now my requirment is to disable all at once . The below are change i did for disable all logges...
8
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I disable the right mouse button? -----------------------------------------------------------------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.