473,326 Members | 2,732 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,326 software developers and data experts.

skip compilation of unused functions

Hello,

I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is
there any way to throw them out of the final executable? Is there maybe
a program that cuts them away from the source code of the dietlibc?
I am using gcc.

Joris
Aug 15 '07 #1
10 2733
Joris Dolderer <no-spamwrote:
I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is
there any way to throw them out of the final executable? Is there maybe
a program that cuts them away from the source code of the dietlibc?
I am using gcc.
Sorry, but that's nothing related to C (or compiling) but solely
depends on how clever the linker is that assembles the final pro-
gram from the object files and the libraries. Perhaps it already
does what you want automatically, especially when you do static
linking. But you will have to ask in some group where the inner
workings of the linker you use are on-topic

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 15 '07 #2
"Joris Dolderer" <no-spamwrote in message
news:46**********************@newsspool3.arcor-online.net...
Hello,

I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is there
any way to throw them out of the final executable? Is there maybe a
program that cuts them away from the source code of the dietlibc?
I am using gcc.
Linking is off-topic on clc.

<OT>
Any decent, modern linker will do exactly what you ask when statically
linking. If yours doesn't, replace it. GCC just uses whatever your
platform's standard linker is, so you'll need to ask for details on a
newsgroup specific to your platform if you're not sure how smart your linker
is.
</OT>

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Aug 15 '07 #3
Stephen Sprunk wrote:
Linking is off-topic on clc.
Which is the appropriate newsgroup
for discussing what the C standard says about linking?

--
pete
Aug 15 '07 #4
pete said:
Stephen Sprunk wrote:
>Linking is off-topic on clc.

Which is the appropriate newsgroup
for discussing what the C standard says about linking?
Well, of course it's comp.lang.c - but the C Standard actually says
precious little about linking. At best, it's a nod towards the
existence of linkers as an integral part of the build process in
compiler-based implementations.

The nitty-gritty of linkers - how to run them and how to tweak them - is
an inherently implementation-specific subject, best dealt with by
newsgroups devoted to the implementations concerned.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 15 '07 #5
"Stephen Sprunk" wrote:
>"Joris Dolderer" wrote:
>I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is there
any way to throw them out of the final executable? Is there maybe a
program that cuts them away from the source code of the dietlibc?
...
Any decent, modern linker will do exactly what you ask when statically
linking. If yours doesn't, replace it.
Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
All object code generated from the compilation of a single source file
is "atomic" as far as the linker is concerned, and must be included in
its totality in the final executable if a single reference is resolved
from that object code.
And, of course, a single file may contain more than one function, and
some of them may not be used.
The only way I know to force a "vanilla" linker to do this, is by
splitting the source code into one file per function before creating
the library. (I had to do some maintenance work on a project that did
just that, and the huge number of source files turned it into an
unmanageable mess.)
Even then, global variables can still create unwanted dependencies
between object modules.

To the OP: There are code analysis tool (I am using PC-Lint) that will
provide a list of unused functions and variables. It may be possible
to use it to drive other tools to remove these unused functions from
the source files (or surround them with #ifdef...#endif,) creating a
customized version of the library that includes no more than what is
actually used. (Hopefully no less either, it may be difficult to know
if a function is called indirectly.)

The details would be, of course, platform specific.

Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Aug 16 '07 #6
Roberto Waltman wrote:
<snip>
Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
<snip>
A counter-example: IAR's xlink does function-level linking. If I
remember correctly, Microsoft's cl has a switch that allows objects to
be linked with function granularity, too.
I suppose, logically function-level linking is possible and not even
that hard to implement as long as the compiler doesn't generate
cross-function jumps for the sake of size optimization.
-- Ark
Aug 16 '07 #7
Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
i think the units are not object files but named sections like .text .bss
etc withing the object file. compilers like TASKING genererate a seperate
section for every function in an object file.

All object code generated from the compilation of a single source file
is "atomic" as far as the linker is concerned, and must be included in
its totality in the final executable if a single reference is resolved
from that object code.
And, of course, a single file may contain more than one function, and
some of them may not be used.
The only way I know to force a "vanilla" linker to do this, is by
splitting the source code into one file per function before creating
the library. (I had to do some maintenance work on a project that did
just that, and the huge number of source files turned it into an
unmanageable mess.)
Even then, global variables can still create unwanted dependencies
between object modules.

To the OP: There are code analysis tool (I am using PC-Lint) that will
provide a list of unused functions and variables. It may be possible
to use it to drive other tools to remove these unused functions from
the source files (or surround them with #ifdef...#endif,) creating a
customized version of the library that includes no more than what is
actually used. (Hopefully no less either, it may be difficult to know
if a function is called indirectly.)
In Code Warrior IDE, the linker gives a list of functions that do not have
references and names them as UNUSED. But it does not remove them. It can be
done manually by the developer.

Aug 16 '07 #8
Roberto Waltman <us****@rwaltman.netwrites:
"Stephen Sprunk" wrote:
>>"Joris Dolderer" wrote:
>>I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is there
any way to throw them out of the final executable? Is there maybe a
program that cuts them away from the source code of the dietlibc?
...
Any decent, modern linker will do exactly what you ask when statically
linking. If yours doesn't, replace it.

Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
<OT>The gcc tools on my system have options that allow a linking
granularity of a "section". This, together with an option that make
the compiler put each function into a separate section would give the
desired result. Of course, it may not work on the OP's platform and
how to do it requires a question in a group aimed at the OP's tool
set</OT>

--
Ben.
Aug 16 '07 #9
On 15 Aug, 23:23, pete <pfil...@mindspring.comwrote:
Stephen Sprunk wrote:
Linking is off-topic on clc.

Which is the appropriate newsgroup
for discussing what the C standard says about linking?

--
pete
I found, there is almost none that you can ask such questions. You
could try comp.unix.programmer, linux.development.apps but these are
too generic for linking. Might try gnu.gcc.help; even though you
usually get an answer, what you ask is most likely off-topic there,
too. Might try the binutils list, but then that's not a newbie list
and you might get ignored.

The best thing to do is read the book linkers & loaders, and read the
GCC manuals, especially as and ld manuals. They come as pdfs. If your
linker is not ld, good luck.

Thanks,
Bahadir

Aug 16 '07 #10
Bahadir <Bi*************@gmail.comwrites:
[...]
The best thing to do is read the book linkers & loaders, and read the
GCC manuals, especially as and ld manuals. They come as pdfs. If your
linker is not ld, good luck.
That would be the book "Linkers and Loaders" by John R. Levine
(moderator of comp.compilers), right? To be clear, that book is
available only on paper as far as I know.

The GNU as and ld manuals are primarily available in "info" format
("info as" or "info ld" if the command is available), but I suppose
you can also get them in pdf.

(This isn't really topical; I'm just clarifying the redirection.)

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 16 '07 #11

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

Similar topics

35
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...
6
by: Dom Gilligan | last post by:
Does anyone know of a C++ lint, preferably free? I've inherited a large code base and need to remove some unused functions. What I've found so far: gcc -O2 does *not* check for unused functions...
11
by: Michael B Allen | last post by:
Is there a standard method for supressing warnings regarding unused parameters? I have a function that might be called hundreds of thousands of times that looks like this: const void *...
9
by: Felix Kater | last post by:
Hi, if I link my code to other o-files: Does the linker include the whole o-file or does it use the needed functions from that only? Felix
17
by: abdur_rab7 | last post by:
Hi, I am compiling code for nanosleep in CYGWIN. During compilation i get the following error Socket.cc: In method `bool Socket::hangOnConnection(int = 0)': Socket.cc:338: aggregate `struct...
11
by: Charles Sullivan | last post by:
I have a number of functions, e.g.: int funct1( int arg1, int arg2, int arg3 ); int funct2( int arg1, int arg2, int arg3 ); int funct3( int arg1, int arg2, int arg3 ); that are called via...
33
by: jacob navia | last post by:
Hi I am considering extending the lcc-win32 compiler to accept int fn(int a, int b,double) { // body of the function } This allows the programmer to specify that the third parameter is not...
6
by: pleexed | last post by:
hello, this is my first post in a newsgroup, i hope i do everything right :) first of all, i am sure there have been a lot of "are templates slow?" questions around, but i think what i would...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.