Is it possible to incorporate one executable program into your Rust code?

So lets say there is a program written (in whatever language) and it is in its executable form. So lets say the program takes 4 arguments. And lets say I want to interact with it in Rust. I could possibly run shell commands but that would mean I would have to have two separate executables. Is there a way to somehow put the executable of the program into my Rust code and then execuate it and pass the arguments within Rust?

You could include the executable using include_bytes!, but you would need to write the array to a temporary file first to execute it.

So lets say used that crate and I have now one executable after compilation. So as soon as I execute my compiled application it would write to the array to the tomporary file before it executes the secondary program (the program that takes 4 arguments)?

You used which crate?

Sorry I meant using this library:


So the very first post (at the top)

Is what I am trying to achieve.

If you have access to the program's source code, you can replace its main to be a regular function, and compile the whole thing as part of your own program.

If the program is in C, you hack it with a C macro.


What about situations where I don't have access it its source code or it was written in another language other than C/Rust?

Does this work with C++ as well by any chance?

Sorry I am kinda noob can I please have an example?

Dude, that's really cool. So simple. :slight_smile:


For arbitrary binaries, it gets messy. You could try to include the executable with include_bytes!, then write it to a temp directory, and execute via Command from there. That should be easy to implement, but not very elegant, and I'd worry that antivirus programs may not like it.

I guess theoretically it'd be possible to parse an executable file, extract the raw machine code from there (and any linking/relocation work it requires), and somehow transplant it to a new binary, but I've never implemented something like this.

If you want to read (a lot) about executable formats to get an idea of why anything beyond "just embed it wholesale and then execute it like it was still a separate binary somehow" is daunting, fasterthanlime has an ongoing series about it (for linux ELF files). You're not going to get a practical solution out of it, but if you like long-form technically-dense articles you might enjoy them.

There may be sub-categories of executables that are less complicated... but I wouldn't want to count on it.

I haven't tried it myself but I think it would be fairly easy to create a "file" that lives only in RAM with memfd_create. So you could include the binary using include_bytes! and write it to an in memory file created by memfd_create and then you could run that executable with the regular Command.

That's a good idea, and it looks ike that's what pentacle does, but that's only going to work on Linux if I understand correctly, which is unfortunate.

This create might help too:

If you're on Windows memexec seems like a good fit.

1 Like