My goal is to generate a *.dll
that contains all the resources the program needs (around 5 Mo worth of data). I use a program of my own that converts all the files in a folder into a big .hpp
such as:
#pragma once
#include <vector>
#include <map>
#include <string>
std::map<std::string, std::vector<uint8_t>> assets {
{"file_A.md", {
0x23, 0x20, 0x2A, 0x2A, 0x43, 0x6F, 0x6E, 0x74, 0x72, 0xC3, 0xB4, 0x6C, 0x65, 0x72, 0x20, 0x6C,
0x27, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, 0x67, 0xC3, 0xA9, 0x6E, 0xC3, 0xA9, 0x72, 0x61}},
{"file_B.md", {
0x24, 0x20, 0x2A, 0x2A, 0x43, 0x6F, 0x6E, 0x74, 0x72, 0xC3, 0xB4, 0x6C, 0x65, 0x72, 0x20, 0x6C,
0x27, 0x61, 0x73, 0x70, 0x65, 0x63, 0x74, 0x20, 0x67, 0xC3, 0xA9, 0x6E, 0xC3, 0xA9, 0x72, 0x61}},
...
...
};
Then I include this file into another project and use the map to iterate through the files.
When the size of the binaries converted are above around 500ko
, mvsc 2019
(driven by CMake
) crashes with the message :
[build] C:Program Files (x86)Microsoft Visual Studio2019ProfessionalMSBuildMicrosoftVCv160Microsoft.CppCommon.targets(687,5): error MSB6006: Arrêt de "CL.exe" avec le code -1073741571. [C:GitHubgtb-visual-control-templatebuildgtb-visual-control-template.vcxproj]
[proc] The command: "C:Program FilesCMakebincmake.EXE" --build c:/GitHub/gtb-visual-control-template/build --config Debug --target ALL_BUILD -j 10 -- exited with code: 1
[build] Build finished with exit code 1
What could be the cause of the problem? Is it related to the type of structure I am using for holding the data ?
2
Answers
Thanks to @Nicol Bolas, I came up to this:
And it works like a charm. Just tried with 20Mo without any issue! I may share the conversion software that takes a folder as input and generates this header if some people want it.
Whenever you use list initialization syntax (curly braces) to initialize an object that has a real constructor in it, that constructor call has to take an
initializer_list
. Thatinitializer_list
object points to an array of values which must exist somewhere. And that "somewhere" tends to be on a stack at some point.So you need to make sure that the braced-init-list does not have so many values that it won’t fit onto the stack.
So instead of directly initializing the
map
with that much data, you need to initialize a static C++ array type, and then use that to initialize themap
.