How can I get gcc to write a file larger than 2.0 GB?

I wanted to recompile 'xxd' (written in C), so I installed CodeBlocks as the IDE.

All seemed to go well unil I discovered that I couldn't write past the 2.0 GB barrier...

I've read that 'gcc' needs to be recompiled... (That sounds a bit dramatic..)
I've read that I can use 'fread64()' instead of 'fread()' ... (didn't work)
I've read something about a compiler options (?)... but I get lost at that point?

I am surprised that it didn't work out-of-the-box, as I thought the 2.0 GB limit was ancient history as far as defaults go ... wrong again?:(

My OS is 32-bit, on 32-bit hardware.
The gcc version report in as: gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

Is there a simple way around this issue?

PS.. I was fascinated by the WARNINGS: section of 'info xxd' (..only on Linux ;)

3 Answers

A simple #define seems to be all that is needed.. (the program writes past 2 GB now.)

Perhaps the reason it didn't compile apppropriately, is that I compiled a single program from a much larger suite ('xxd' is part of 'vim')...

Had I compiled 'vim' in toto, it would most likely have worked fine...

So for anyone who comes to this page, the following may be of some value:
I assume similar settings would apply to other IDEs.

Adding #defines
* Using CodeBocks (as a global setting) ===================================== Settings Compiler and Debugger... [Compiler Settings] [#defines] ... Add the following _FILE_OFFSET_BITS="64"
* Using CodeBlocks (for a given Project) ====================================== Properties Build Options [Compiler Settings] [#defines] ... Add the following _FILE_OFFSET_BITS="64"
* Directly into gcc's command line ================================ gcc -D_FILE_OFFSET_BITS="64"
* Add a #define directly to the source ==================================== #define _FILE_OFFSET_BITS 64 

Also, I discovered this snippet while googling for the solution...
What macros are predefined by gcc? ... in the terminal:

  • touch foo.h; cpp -dM foo.h
2

I recommend adding -D_GNU_SOURCE as long as #include <features.h> is used. This will enable all the largefile support. Read the beginning of /usr/include/features.h for more details:

...
_LARGEFILE_SOURCE Some more functions for correct standard I/O.
_LARGEFILE64_SOURCE Additional functionality from LFS for large files.
_FILE_OFFSET_BITS=N Select default filesystem interface.
....
_GNU_SOURCE All of the above, plus GNU extensions.
1

I had the same issue -- was not able to write a file larger than 2GB. My OS was 64 bit, mount point was jfs2, and my fsize=-1 All I did was change fopen in my program to fopen64 and voila!

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like