How to make an external text file a part of the executable?

Basically I want to include a help routine in my program, one option I have is to use a lot of printf inside a help function. I was wondering if I could instead write my instructions in a text file such that it reads it and prints it out, but in that case I would have to pass the file around with the executable. Is there a way to make the file resource a part of the executable itself?

Compiler: MSVC

1 answer

  • answered 2020-07-05 02:36 SomeUsername1

    Sure, the compiler for C-like languages puts static strings in the .text section (it may be named differently depending on your target arch/the corresponding assembly specification). You just need to format it properly and assign it to a static string then it will be put into the executable by the compiler.

    C++ example:

    #include <iostream>
    static std::string help_text = "Hi, I'm a help text\n"
                                    "-a do this\n"
                                    "-b do that\n"
                                    "-a do this\n"
                                    "-b do that\n";
    int main(const int argc, const char *argv[]) {
        if (argc != 2) {
            std::cout << "Usage: " << help_text << std::endl;

    Likewise, define the string in a header file and include that header file into the main to keep the main file clean.

    In response to the comment a tiny nasm x86 SystemV example:

    mov    rdi, vmsg  ; move whats to be printed to the destination index register 
    mov    rsi, args_to_printf ; e.g. when using %d or sth. to the source index register
    extern printf
    call   printf

    Effectively it makes no difference, it's just a way to move the text to an extra file but still including it into the static part of the executable.