linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Does GCC link only used functions?
@ 2006-01-29  9:08 Shriramana Sharma
  2006-01-29 16:23 ` Steve Graegert
  2006-01-29 18:17 ` Glynn Clements
  0 siblings, 2 replies; 5+ messages in thread
From: Shriramana Sharma @ 2006-01-29  9:08 UTC (permalink / raw)
  To: Linux C Programming List

Hello list. I am relative newbie to GCC.

I have a C program which #includes a header file which in turn #includes many 
other header files. These header files are part of an open source library set 
I got from the net.

Now I do not use all the functions in the various libraries, but the makefile 
which came with the library of course has compile instructions for all the 
libraries. I have modified the makefile to compile my program using those 
libraries, but I did not remove the references to the libraries I knew I did 
not use, for I feared that some error may be caused.

But later I experimented removing some of the superfluous ones from the 
makefile and again "make"-d, and I got the same size executable as before.

So does this mean that GCC links only those functions from a library which I 
call directly or indirectly through another called function in the course of 
my program?

-- 

Penguin #395953 resides at http://samvit.org 
subsisting on SUSE Linux 10.0 with KDE 3.5

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Does GCC link only used functions?
  2006-01-29  9:08 Does GCC link only used functions? Shriramana Sharma
@ 2006-01-29 16:23 ` Steve Graegert
  2006-01-29 18:17 ` Glynn Clements
  1 sibling, 0 replies; 5+ messages in thread
From: Steve Graegert @ 2006-01-29 16:23 UTC (permalink / raw)
  To: linux-c-programming

On 1/29/06, Shriramana Sharma <samjnaa@gmail.com> wrote:
> Hello list. I am relative newbie to GCC.
>
> I have a C program which #includes a header file which in turn #includes many
> other header files. These header files are part of an open source library set
> I got from the net.
>
> Now I do not use all the functions in the various libraries, but the makefile
> which came with the library of course has compile instructions for all the
> libraries. I have modified the makefile to compile my program using those
> libraries, but I did not remove the references to the libraries I knew I did
> not use, for I feared that some error may be caused.
>
> But later I experimented removing some of the superfluous ones from the
> makefile and again "make"-d, and I got the same size executable as before.
>
> So does this mean that GCC links only those functions from a library which I
> call directly or indirectly through another called function in the course of
> my program?

Yes,  everything not needed for program execution is simply dropped in
the stage of linking.

	\Steve

--

Steve Graegert <graegerts@gmail.com>
Software Consultant {C/C++ && Java && .NET}
Office: +49 9131 7123988
Mobile: +49 1520 9289212

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Does GCC link only used functions?
  2006-01-29  9:08 Does GCC link only used functions? Shriramana Sharma
  2006-01-29 16:23 ` Steve Graegert
@ 2006-01-29 18:17 ` Glynn Clements
  2006-02-02  5:17   ` Hendrik Visage
  1 sibling, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2006-01-29 18:17 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: Linux C Programming List


Shriramana Sharma wrote:

> I have a C program which #includes a header file which in turn #includes many 
> other header files. These header files are part of an open source library set 
> I got from the net.
> 
> Now I do not use all the functions in the various libraries, but the makefile 
> which came with the library of course has compile instructions for all the 
> libraries. I have modified the makefile to compile my program using those 
> libraries, but I did not remove the references to the libraries I knew I did 
> not use, for I feared that some error may be caused.
> 
> But later I experimented removing some of the superfluous ones from the 
> makefile and again "make"-d, and I got the same size executable as before.
> 
> So does this mean that GCC links only those functions from a library which I 
> call directly or indirectly through another called function in the course of 
> my program?

Are you referring to shared libraries or static libraries?

For a shared library, the linker simply stores references to the
library functions; it won't copy any object code into your executable. 
The loader will map the shared library into the process' address space
when the program is executed.

For a static library (which is just an archive of object files), the
linker will only use those object files which are necessary to satisfy
any outstanding references.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Does GCC link only used functions?
  2006-01-29 18:17 ` Glynn Clements
@ 2006-02-02  5:17   ` Hendrik Visage
  2006-02-02 16:39     ` Glynn Clements
  0 siblings, 1 reply; 5+ messages in thread
From: Hendrik Visage @ 2006-02-02  5:17 UTC (permalink / raw)
  To: Glynn Clements; +Cc: Shriramana Sharma, Linux C Programming List

On 1/29/06, Glynn Clements <glynn@gclements.plus.com> wrote:
>
> Shriramana Sharma wrote:

> > So does this mean that GCC links only those functions from a library which I
> > call directly or indirectly through another called function in the course of
> > my program?

> For a static library (which is just an archive of object files), the
> linker will only use those object files which are necessary to satisfy
> any outstanding references.

That means that all the functions and everything in that object file
is included?
thus given the lib.a contains obj1.o and obj2.o compiled from obj1.c &
obj2.c below,
the program main from main.c below, linked with lib.a will contain the
obj1.o part, which
will also include the superflouos int b() (That's the way I understood
it too), but not obj2 with c() & d(). that's the reason why static
libraries makes use of small objects to minimize the stuff linked in.

obj1.c:
int a(){return 1;}
int b(){return 2;}

obj2.c
int c(){return 3;}
int d(){return 4;}

main.c
int main(){
 return a();
}

--
Hendrik Visage

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Does GCC link only used functions?
  2006-02-02  5:17   ` Hendrik Visage
@ 2006-02-02 16:39     ` Glynn Clements
  0 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2006-02-02 16:39 UTC (permalink / raw)
  To: Hendrik Visage; +Cc: Shriramana Sharma, Linux C Programming List


Hendrik Visage wrote:

> > > So does this mean that GCC links only those functions from a library which I
> > > call directly or indirectly through another called function in the course of
> > > my program?
> 
> > For a static library (which is just an archive of object files), the
> > linker will only use those object files which are necessary to satisfy
> > any outstanding references.
> 
> That means that all the functions and everything in that object file
> is included?

Yes. The linker can't include specific portions of an object file.

> thus given the lib.a contains obj1.o and obj2.o compiled from obj1.c &
> obj2.c below,
> the program main from main.c below, linked with lib.a will contain the
> obj1.o part, which
> will also include the superflouos int b() (That's the way I understood
> it too), but not obj2 with c() & d().

Correct.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-02-02 16:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-29  9:08 Does GCC link only used functions? Shriramana Sharma
2006-01-29 16:23 ` Steve Graegert
2006-01-29 18:17 ` Glynn Clements
2006-02-02  5:17   ` Hendrik Visage
2006-02-02 16:39     ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).