Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will g++ link my programs with classes it doesn't use from a library?

I've created a simple static library, contained in a .a file. I might use it in a variety of projects, some of which simply will not need 90% of it. For example, if I want to use neural networks, which are a part of my library, on an AVR microcomputer, I probably wont need a tonne of other stuff, but will that be linked in my code potentially generating a rather large file?

I intend to compile programs like this:

g++ myProg.cpp myLib.a -o prog

like image 890
corazza Avatar asked Sep 29 '12 11:09

corazza


1 Answers

G++ will pull in only the object files it needs from your library, but this means that if one symbol from a single object file is used, everything in that object file gets added to your executable.

One source file becomes one object file, so it makes sense to logically group things together only when they are sure to be needed together.

This practice varies by compiler (actually by linker). For example, the Microsoft linker will pick object files apart and only include those parts that actually are needed.

like image 150
mah Avatar answered Nov 15 '22 16:11

mah