473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9915

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
"RedDevilDa n" <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.progr ammer.

--
Keith Thompson (The_Other_Keit h) 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.progr ammer

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, "RedDevilDa n" <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
"RedDevilDa n" <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.stanfor d.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

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

Similar topics

5
5983
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 disactivate other checkboxes. How can I do this ? here is the code:
6
3163
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 href="#">bahbahbah</a> Does not work, as it is still clickable. It only changes the color to grey.
12
8883
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
14576
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 textbox enabled and text/background were intact but mouse cursor remained an arraw (as opposed to an I for editable). I tried a Panel control on my .net form since I could I guess there is no longer a frame control (also tried a groupbox control)....
4
3125
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: function disable() { alert("Forms length is :" + Form1.length); for (i = 0; i < Form1.length; i++) { var formElement = Form1.elements;
0
1804
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 Web Form,Web Form1.aspx and Web Form2.aspx,Now From Web Form1.aspx When I Click One Button (Disable), Then Web Form2.aspx Buttons
4
14246
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 i could not find ways to disable/enable, (listing is however, possible). Where should I look for to enable/disable devices in python. Thanks,
0
2164
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 ! Observe log4j parsing this file log4j.debug=false
8
3724
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I disable the right mouse button? ----------------------------------------------------------------------- The oncontextmenu intrinsic event is the only safe and reliable method. Of the other approaches often presented, most depend on an alert box interrupting the process and rarely work. Note that oncontextmenu is a non-standard event and is not...
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.