The following example C++ project has two files: hello_world.sh and hello_world.cpp.
I want to embed the "hello_world.sh" as resource file when building the project with cmake, after building the project, an executable called "helloworld" is generated.
Here is src/hello_world.sh
#!/bin/bash
echo "Hello World in hello_world.sh"
Here is src/hello_world.cpp
#include <iostream>
int main()
{
std::cout << "Hello World in hello_world.cpp" << std::endl;
/** How to call hello_world.sh in the main() function? */
/** Dummy Code **/
// Use /bin/bash to run the hello_world.sh
// which is embedded into the "helloworld" executable when building with cmake
/bin/bash /path/to/resource_inside_the_helloworld_executable/hello_world.sh
}
Build the project, and embed the "hello_world.sh" into the generated executable file "helloworld":
# ... I don't know how to build this project to include "hello_world.sh" as resource file ...
Here is the expected output when running "helloworld" from terminal:
# helloworld
Hello World in hello_world.cpp
Hello World in hello_world.sh
I know I can install the "hello_world.sh" into the /home/test/hello_world.sh and then run it with "/bin/bash /home/test/hello_world.sh". But I just want to embed the "hello_world.sh" into the executable "helloworld" as a resource and call "hello_world.sh" internally from "helloworld".
Is it possible? If yes, how to do it?
c++code, you can use std::systemstd::system("/bin/bash /home/test/hello_world.sh"), but the problem is: How to embed thehello_world.shas resource and run thehello_world.sh? How to tellstd:systemto get the bash script from resourcestd::system("/bin/bash /not/a/real/path/which/is/embedded/into/the/executable/as/resource/hello_world.sh")?cmaketarget), but I can't see a way to call it from thec++code. Note that you could still write this array in a temporary file and then usestd::system, but it is seems quite tedious.