I programmed a game in c++. Now I want to pack the finished product. Game runs fine from within the compiler. But how can i link the assets(like images, music etc) of the game to the exe ? When I run the executable, it says it is not able to load any assets and terminates immediately. How can I bind the assets with the executable ?
There's no general way to do this, especially not cross-platform.
Do you really have to pack your ressources into the executable file? I'd consider deploying a small setup program instead, which would unpack the ressources next to the executable file, which is essentially what you have when running from your IDE/when debugging.
Just because you've added some file to your project doesn't mean it's actually compiled into your executabe (an impression MSVC might give you for example; there are different "build actions" for files added to the project).
Also consider making your executable bigger and reading ressources from it might slow down your program, since overpedantic antivirus solutions might cause repeated scans.
So if you really insist on packing everything into your executable, I'd do something like this:
- Compile a list of all required assets.
- Write a second tool to "pack" the assets:
- Possibly compress and append the ressources to the final executable file, starting with some kind of index file containing all the offsets/sizes-
- Write the starting offset of the ressources at the end of the resulting file.
Your actual program can then access ressources this way:
- Open the executable in readonly mode (random access will be blocked).
- Read the starting offset from the end of the file.
- Move to the offset and read the index file.
- Load your assets according to the index file.
Please note that this approach has its flaws, e.g. you won't be able to sign your final binary (since that would once again change the layout). There are workarounds, but it would overcomplicate things here I guess.
Of course you can use platform specific loading of embedded resources if you want to, but this won't be a cross-platform solution and requires further information regarding your goals.
TL;DR: Is there really some reason to do so? If you want easy deployment, use a setup programm/installer (like NSIS or InnoSetup). If you want to compress/pack ressources, do so in a separate file.