Output File Size Issue (Solved!)

Hello everyone,

I am writing a .dll for a separate project and have run into a problem regarding the file size of the dll. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
#include <fstream>
#define DLLEXPORT extern "C" __declspec(dllexport)
using namespace std;

// Opens the file
DLLEXPORT bool openFile( char* fileName )
{
    ifstream file;
    file.open( fileName, ifstream::in );
    return file.is_open();
}


When I compile this code, I get a file size of 455.50KB. When I comment out lines 9 and 10, my file size stays about the same. If I comment out line 8, my file size goes down to 5.50KB.

Why does declaring
ifstream file;
add an extra 450KB to the file, and how can I shrink it? I have no compiler flags set. I'm using Code::Blocks 10.05 with MinGW.

(Yes, I am aware the code doesn't do anything useful. It's just a test for now)
Last edited on
Using C++ streams in MinGW has a relatively large size overhead.
Streams are template classes, and template classes and functions don't generate any code if they're not used. That's why not declaring 'file' leaves you with a much smaller binary.

I had this very some problem the other day. Your options are:
1. Use the -s compiler option. This will reduce the file size by about 2/5.
2. Link dynamically. I don't know if this is possible with MinGW, but it's the default with VC++. Linking dynamically has certain drawbacks, though.
3. Don't use C++ headers. This is what I did. Instead, use C headers.

C++ is a great language, but some implementations don't behave nicely with minuscule programs.
Thanks a lot for the quick reply! I switched to the C file functions and my output file is now only 6 KB.
Topic archived. No new replies allowed.