2

I'm trying to compile a simple C++ file that does:

static const unsigned char content[] = 
        {
              // 29MB of data written as "0x1E, 0x83, 0x3E, 0x86, 0xC8, 0x80, ...". 10 values per line
        };

The file to compile has almost 3M lines and is ~200Mo

This is an output from a ressources management system, trying to embed in my binary a 29Mo ressource file (let's assume it's a video file) for later use at runtime.

When I compile, VS2015 reports:

fatal error C1060: compiler is out of heap space

Is there any alternative to that (use something else than a static const unsigned char)? Make the file have less lines but more characters per line? ...

Note: I tried /Zm1000 option without success.

trincot
  • 317,000
  • 35
  • 244
  • 286
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • 1
    I assume by Mo you mean MB? Why does that have to be in the sourcecode? – Baum mit Augen Jul 13 '18 at 10:59
  • 1
    What does _29Mo_ mean? Please fix your post. – Ron Jul 13 '18 at 10:59
  • Is this static variable global or inside function? – bartop Jul 13 '18 at 10:59
  • 3
    This is bordering on an [X/Y Problem](http://xyproblem.info/). – PaulMcKenzie Jul 13 '18 at 11:00
  • @BaummitAugen: Yes MB (Mo is in French). I need the file to be embedded in the binary to make it easier to deploy the binary on any machine. Also the user must not be allowed to modify the file used at runtime...so embedding it in the binary makes it being kind of "read-only". – jpo38 Jul 13 '18 at 11:05
  • 5
    You could try to embed the binary file as a resource. Start with https://msdn.microsoft.com/en-us/library/windows/desktop/ms632583.aspx – manuell Jul 13 '18 at 11:05
  • @bartop: Inside a constructor actually – jpo38 Jul 13 '18 at 11:06
  • 1
    @jpo38 The resource type `RT_RCDATA` is probably what you would be aiming to use. Let the resource compiler (`rc.exe`) handle it at build time, not the C++ compiler. The C++ code would then just need to read that resource into a dynamically allocated buffer. – PaulMcKenzie Jul 13 '18 at 11:21
  • 1
    @PaulMcKenzie: Unfortunately, this code is also compiled using QtCreator for Android (even if it will likely not run here with so many memory being used). So using rc is not an option for me... – jpo38 Jul 13 '18 at 11:29
  • 3
    The 64-bit compiler might be able to tackle it. Change the VC++ Directories > Executable Directories setting to pick either $(VC_ExecutablePath_x64_x86), x64_x64 or x64_ARM, depending on the desired platform target. Can't be much fun watching that grass grow, you are almost certainly going to change that utility to write a binary file. – Hans Passant Jul 13 '18 at 11:30
  • 1
    @jpo38 I have not used Qt, but does [this link](http://doc.qt.io/qt-5/resources.html) help (see *Compiled-In Resources*)? – PaulMcKenzie Jul 13 '18 at 11:41
  • @PaulMcKenzie: Qrc's generated C++ file also ends up with "compiler is out of heap space" error.... – jpo38 Jul 13 '18 at 11:58
  • Switching to VS2017 might help (although I don't think it will). – Jabberwocky Jul 13 '18 at 12:02
  • 1
    Another option would be to append the binary data to the end of the .exe file and extract the data from the .exe file during runtime, but not sure if you can do this on Android. – Jabberwocky Jul 13 '18 at 12:05
  • 1
    @HansPassant: I had $(VC_ExecutablePath_x64). Replacing it by $(VC_ExecutablePath_x64_x64) fixed the issue! Thanks, you may want to post this as a real answer. Any idea how this option can be changed from CMake (My VS solution is generated from CMake). – jpo38 Jul 13 '18 at 12:28
  • I have no idea. Be sure to include it in your own answer once you figure it out. – Hans Passant Jul 13 '18 at 12:32
  • @HansPassant: My original question did not mention CMake, so I will accept your answer even if it does not tell how to do this within CMake generator. – jpo38 Jul 13 '18 at 12:33
  • Here's another SO question on embedding binary data into an object file: https://stackoverflow.com/questions/12997569/commandline-tool-for-embedding-binary-file-into-obj-file – Eljay Jul 13 '18 at 15:54

1 Answers1

2

As per MSDN documentation on the subject:

Eliminate unnecessary global variables, for example, by allocating memory dynamically instead of declaring a large array.

and

Split the current file into smaller files.

Both of which you should fix.

Ron
  • 14,674
  • 4
  • 34
  • 47