473,883 Members | 2,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

automatically remove unused #includes from C source?

Hi All,

Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?

Thanks,
Sean

Oct 20 '06 #1
31 8641
In article <11************ *********@k70g2 000cwa.googlegr oups.com>,
<sm*********@gm ail.comwrote:
>Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?
Tricky.

A #define in include1.h might be used in a #define in include2.h that
might be used to build a type in include3.h that might be needed by a
function declaration brought in by include5.h that is #include'd by
include4.h, and the function name might be in a disguised array
initialization form in include6.h and the analyzer would have to
analyze your source to see whether you refer to that function directly
or if you use the array initialization. ..

In other words, such a tool would have to pretty much be a C compiler
itself, but one that kept track of all the "influences " that went
to build up every token, and figured out what wasn't used after-all.

It might be easier just to start commenting out #include's and
seeing if any compile problems came up.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Oct 20 '06 #2
On Fri, 20 Oct 2006 17:39:25 +0000 (UTC), Walter Roberson wrote:
<sm*********@gm ail.comwrote:
>>Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?

It might be easier just to start commenting out #include's and
seeing if any compile problems came up.
Automate that and you have the requested tool!

Best wishes,
Roland Pibinger
Oct 20 '06 #3
In article <45************ **@news.utanet. at>,
Roland Pibinger <rp*****@yahoo. comwrote:
>On Fri, 20 Oct 2006 17:39:25 +0000 (UTC), Walter Roberson wrote:
><sm*********@g mail.comwrote:
>>>Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?
>>It might be easier just to start commenting out #include's and
seeing if any compile problems came up.
>Automate that and you have the requested tool!
including a particular file can end up changing the meaning of
something else, but the code might compile fine without it.

For example, you might have an include file that contained

#define _use_search_heu ristics 1

Then the code might have

#if defined(_use_se arch_heuristics )
/* do it one way */
#else
/* do it a different way */
#endif

where the code is valid either way.

Thus in order to test whether any particular #include is really
needed by checking the compile results, you need to analyze the
compiled object, strip out symbol tables and debug information and
compile timestamps and so on, and compare the generated code.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Oct 20 '06 #4
On Fri, 20 Oct 2006 19:47:42 +0000 (UTC), Walter Roberson wrote:
>including a particular file can end up changing the meaning of
something else, but the code might compile fine without it.

For example, you might have an include file that contained

#define _use_search_heu ristics 1

Then the code might have

#if defined(_use_se arch_heuristics )
/* do it one way */
#else
/* do it a different way */
#endif

where the code is valid either way.
You are right in theory. But that kind of include file dependencies
(include order dependencies) is usally considered bad style.
>Thus in order to test whether any particular #include is really
needed by checking the compile results, you need to analyze the
compiled object, strip out symbol tables and debug information and
compile timestamps and so on, and compare the generated code.
IMO, this is overdone. You have to test your application after code
changes anyway.

Best regards,
Roland Pibinger
Oct 20 '06 #5
On Fri, 20 Oct 2006 20:30:35 GMT, rp*****@yahoo.c om (Roland Pibinger)
wrote:
>On Fri, 20 Oct 2006 19:47:42 +0000 (UTC), Walter Roberson wrote:
>>including a particular file can end up changing the meaning of
something else, but the code might compile fine without it.

For example, you might have an include file that contained

#define _use_search_heu ristics 1

Then the code might have

#if defined(_use_se arch_heuristics )
/* do it one way */
#else
/* do it a different way */
#endif

where the code is valid either way.

You are right in theory. But that kind of include file dependencies
(include order dependencies) is usally considered bad style.
No, he's right in practice. There's no guarantee that a body of
existing code will conform to your (or anyone's) rules of good style.
>
>>Thus in order to test whether any particular #include is really
needed by checking the compile results, you need to analyze the
compiled object, strip out symbol tables and debug information and
compile timestamps and so on, and compare the generated code.

IMO, this is overdone. You have to test your application after code
changes anyway.

Best regards,
Roland Pibinger
--
Al Balmer
Sun City, AZ
Oct 20 '06 #6
In article <45************ **@news.utanet. at>,
Roland Pibinger <rp*****@yahoo. comwrote:
>On Fri, 20 Oct 2006 19:47:42 +0000 (UTC), Walter Roberson wrote:
>>including a particular file can end up changing the meaning of
something else, but the code might compile fine without it.
>>For example, you might have an include file that contained
#define _use_search_heu ristics 1
Then the code might have
#if defined(_use_se arch_heuristics )
/* do it one way */
#else
/* do it a different way */
#endif
where the code is valid either way.
>You are right in theory. But that kind of include file dependencies
(include order dependencies) is usally considered bad style.
It happens often with large projects with automakes and
system dependancies. The included file that changes the meaning
of the rest is a "hints" file.

For example, on the OS I use most often, for a well
known large project (perl as I recall) the autoconfigure
detects that the OS has library entries and include entries
for a particular feature. Unfortunately that particular feature
doesn't work very well in the OS -- broken -and- very inefficient.
So the OS hints file basically says, "Yes I know you've detected
that, but don't use it." So the large project goes aheads and
compiles in the code that performs the task using more standardized
system calls instead of the newer less-standardized API.
>IMO, this is overdone. You have to test your application after code
changes
Conformance tests can take 3 days per build, and if you
are checking whether a project with 1500 #includes (distributed
over the source) can survive deleting one particular include
out of one particular module, then you need up to pow(2,1500)
complete builds and conformance tests. Even if each *complete*
application conformance test took only 1 second, it'd take
10^444 CPU years to complete the testing. *Much* faster to break
it into chunks (e.g., by source file) and check to see whether
each chunk still produces the same code after removal of a
particular include: the timing then becomes proportional to
the sum of pow(2,includes_ in_this_chunk) instead of the product
of those as would be the case with what you propose.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Oct 20 '06 #7
Walter Roberson wrote:
Conformance tests can take 3 days per build, and if you
are checking whether a project with 1500 #includes (distributed
over the source) can survive deleting one particular include
out of one particular module, then you need up to pow(2,1500)
complete builds and conformance tests. Even if each *complete*
application conformance test took only 1 second, it'd take
10^444 CPU years to complete the testing. *Much* faster to break
it into chunks (e.g., by source file) and check to see whether
each chunk still produces the same code after removal of a
particular include: the timing then becomes proportional to
the sum of pow(2,includes_ in_this_chunk) instead of the product
of those as would be the case with what you propose.
That is a good idea: selectively removing #include statements, and then
simply seeing if the resulting object code file changes.

Otherwise, a customized C compiler could absolutely tell if there were
any dependencies on a particular #include file.
Oct 21 '06 #8

"Walter Roberson" <ro******@ibd.n rc-cnrc.gc.cawrote in message news:eh******** **@canopus.cc.u manitoba.ca...
In article <45************ **@news.utanet. at>,
Roland Pibinger <rp*****@yahoo. comwrote:
>>On Fri, 20 Oct 2006 17:39:25 +0000 (UTC), Walter Roberson wrote:
>><sm*********@ gmail.comwrote:
Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?
>>>It might be easier just to start commenting out #include's and
seeing if any compile problems came up.
>>Automate that and you have the requested tool!

including a particular file can end up changing the meaning of
something else, but the code might compile fine without it.

For example, you might have an include file that contained

#define _use_search_heu ristics 1

Then the code might have

#if defined(_use_se arch_heuristics )
/* do it one way */
#else
/* do it a different way */
#endif

where the code is valid either way.

Thus in order to test whether any particular #include is really
needed by checking the compile results, you need to analyze the
compiled object, strip out symbol tables and debug information and
compile timestamps and so on, and compare the generated code.
Then, analyze it to make sure you don't delete the #include of "seems_unused.h " in this:

seems_unused.h:
-----------------
#define MIGHT_NEED 1

somefile.c:
----------
#ifdef DEFINED_WITH_MI NUS_D
int var = MIGHT_NEED;
#endif

-- so that next week, when somebody does gcc -DDEFINED_WITH_M INUS_D, the code still builds.
Oct 21 '06 #9
sm*********@gma il.com wrote:
Hi All,

Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?

Thanks,
Sean
doesn't PC-LINT give you a list of unused includes?
Oct 21 '06 #10

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

Similar topics

4
2302
by: Sam Smith | last post by:
Hi, I'm thinking about whether or not to build a general library on its own and later link that lib with the main executable, or to build the main executable with a subset of the relevant source files from that library. If I choose to build the executable directly with only relevant source files, I know I do not bloat the executable with symbols and functions that are never used.
35
1254
by: Geronimo W. Christ Esq | last post by:
Are there any scripts or tools out there that could look recursively through a group of C/C++ source files, and allow unreferenced function calls or values to be easily identified ? LXR is handy for indexing source code, and for a given function or global variable it can show you all the places where it is referenced. It would be really nice to have a tool that would simply list all of the referenced functions, so that you could go...
8
2245
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need to have all the prior dates pop in. Example: User enters 2/9/05 in the date field at the top being a Wednesday...I need the dates at the bottom to be filled in automatically, respectively as 2/3/05, 2/4/05, 2/5/05, 2/6/05, 2/7/05, 2/8/05, 2/9/05...
6
7002
by: Jozef | last post by:
Hello, Is there any way to automatically create an ODBC data source if it doesn't already exist? I'd like to deploy this program on workstations that I may not have access to, and don't want to rely on the user for any technical expertise. Is there any way to do that? Thanks!
3
3073
by: Apollo440 | last post by:
How to remove unused using directive? eg) using System; using System.Windows.Forms; <- i want to remove this unused using directive namespace foo { :
4
7138
by: Maxim | last post by:
Greetings, In my C# project, I'm using a third-party Opensource library... in source code form. I mean no assemblies, just pure source code. It allows me to add new application-specific functionality to that library, finetune it, and see how things work. Well, the problem is that the library is quite huge, so the application's size gets not really acceptable - I'm developping for an
2
4017
by: AussieRules | last post by:
Hi, There use to be some 3rd party tools that would remove unused subs and function and unused code from vb projects. Is there are a way to do this in vb.net 03
3
7200
by: Tom McL. | last post by:
Is there a way I identify and remove unused variable within vb2005 standard edition
100
5170
by: jacob navia | last post by:
Recently, a heated debate started because of poor mr heathfield was unable to compile a program with // comments. Here is a utility for him, so that he can (at last) compile my programs :-) More seriously, this code takes 560 bytes. Amazing isn't it? C is very ompact, you can do great things in a few bytes. Obviously I have avoided here, in consideration for his pedantic
0
9944
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
9797
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
11154
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...
0
10762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
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
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7977
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
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.