How do I embed my file into an executable and how do I retrieve my resources from it?
iamalinaji opened this issue · 6 comments
Hi, I hope you are doing well.
What I don't understand is how it's possible to embed arbitrary files into my executable. Would you mind explaining that to me? Of course I followed the exact method that was in the README.md file. However, the size of my executable didn't change no matter how large my resources were.
There is my project structure:
-CMakeLists.txt
-CMakeRC.cmake
-resources/
Photo.jpg
My configuration for CMake:
cmake_minimum_required(VERSION 3.10)
project(AddResource)#========== Global Configurations
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)set(CMAKE_CXX_EXTENSIONS OFF)
include(CMakeRC.cmake)
cmrc_add_resource_library( app1-resources
ALIAS app1::rc NAMESPACE app1 resources/archive.zip resources/photo.jpg )
add_executable(MyExe code.cpp)
target_link_libraries(MyExe app1::rc)
My code is:
#include <stdio.h>
#include <cmrc/cmrc.hpp>
CMRC_DECLARE(app1);
int main()
{
getchar();
return 0;
}
The linker will discard your resources since you are not referring to them in your application. You need to call cmrc::app1::get_filesystem()
in your program to gain access to the embedded resources. Check the embedded_filesystem
API document in the README file.
Thanks:)
I have one more question and I would really appreciate it if you hint me about it a bit. If I have an exe file for example somefile.exe, how could I retrieve my resources from it?
The object returned by get_filesystem()
encodes the filesystem structure of your original resources. You just need to call open()
with the path to the resource.
Thanks:)
So I built an executable with one embedded resource which was a zip file. In another project I tried to read the zip file but unfortunately I got an error...
My code:
#include <cmrc/cmrc.hpp>
#include <iostream>
CMRC_DECLARE(app1);
int main()
{
auto fs = cmrc::app1::get_filesystem();
auto archive = fs.open("archive.zip");
return 0;
}
My CMake Configuration file:
cmake_minimum_required(VERSION 3.10)
project(shelter)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include(CMakeRC.cmake)
cmrc_add_resource_library(
app1-resources
ALIAS app1::rc
NAMESPACE app1
myprogram.exe
)
add_executable(myprogram code.cpp)
target_link_libraries(myprogram PRIVATE app1::rc)
What am I doing wrong ?
Thanks in advance for helping me!
It appears that you are not embedding the Zip file in your cmrc_add_resource_library
call. You are passing myprogram.exe
, which is likely not what you want to do.
I think I misunderstood the function of this module, however, thank you for your help!