Skip to contentSkip to conversation

Does GCC link only used functions?

DISCUSSION Shriramana Sharma 4 participants 5 messages Jan 29, 2006 – Feb 2, 2006

lore
Share & tools
lore
#0 lore raw

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?

Signature · 2 lines
Penguin #395953 resides at http://samvit.org 
subsisting on SUSE Linux 10.0 with KDE 3.5
#1 lore raw
Quoted 12 lines · Shriramana Sharma

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.

Quoted 6 lines

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 <[email protected]>
Software Consultant {C/C++ && Java && .NET}
Office: +49 9131 7123988
Mobile: +49 1520 9289212

#2 lore raw
In reply to Shriramana Sharma
Quoted 10 lines · Shriramana Sharma

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.

Quoted 6 lines

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.

Signature · 1 line
Glynn Clements <[email protected]>
#3 lore raw
Quoted 10 lines · Glynn Clements
Quoted 3 lines · Shriramana Sharma

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

#4 lore raw
Quoted 10 lines · Hendrik Visage
Quoted 7 lines
Quoted 3 lines

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.

Quoted 6 lines

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.

Signature · 1 line
Glynn Clements <[email protected]>