I have a rather simple C++ project, which uses boost::regex library. The output I’m getting is 3.5Mb in size. As I understand I’m statically linking all boost .CPP files, including all functions/methods. Maybe it’s possible somehow to instruct my linker to use only necessary elements from boost, not all of them?
$ c++ —version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
This is what size says:
$ size a.out
__TEXT __DATA __OBJC others dec hex
1556480 69632 0 4296504912 4298131024 100304650
I tried strip:
$ ls -al
... 3946688 May 21 13:20 a.out
$ strip a.out
$ ls -al
... 3847248 May 21 13:20 a.out
ps. This is how my code is organized (maybe this is the main cause of the problem):
// file MyClass.h
class MyClass {
void f();
};
#include "MyClassImpl.h"
// file MyClassImpl.h
void MyClass::f() {
// implementation...
}
// file main.cpp
#include "MyClass.h"
int main(int ac, char** av) {
MyClass c;
c.f();
}
What do you think?
Did you compile with debugging symbols enabled? That could account for a large portion of the size. Also how are you determining the size of the binary? Assuming you’re on a UNIX-like platform are you using a straight “
ls -l” or the “size” command. The two could give greatly different results if the binary contains debugging symbols. For example, here are the results I get when building the Boost.Regex “credit_card_example.cpp” example.Similar results occur when just generating the object file:
EDIT: Added some static linking results …
Here’s the binary size when statically linking. It’s closer to what you’re getting:
It’s also possible that much of the large size is coming from other libraries the Boost.Regex library depends on. On my Ubuntu box, the dependencies for the Boost.Regex shared library are the following:
The ICU libraries can get quite large. Besides debugging symbols, perhaps they are the primary contributors to the size of your binary. Furthermore, in the statically linked case, it looks like the Boost.Regex library itself is comprised of large object files:
You could get up to ~600K coming from Boost.Regex alone if some or all of those object files get linked into your binary.