0

I have 3 C++ files that I would like to generate using an executable file, I mean if I click on the executable my 3 files should appear beside it.

So I thought about using ofstream and then create my three files like this. But the problem is, my 3 files contain a lot of lines and escaping the " and ' will take a lot of time...

How can I embed C++ source code inside my executable without the hassle of escaping the string literals?

7
  • 9
    Use raw string literals. Commented Sep 14, 2017 at 14:23
  • But how ? do you have a tutorial ? Thank you Commented Sep 14, 2017 at 14:28
  • And with string literals is not a good idea because in my code I have a lot of " and if I use the strings I will have a lot of problems for instance if in my code I have std::string a = "std::cout << "Hello"<<std::endl;" I would have not std::cout << "Hello"<<std::endl; Commented Sep 14, 2017 at 14:33
  • 3
    @VickyHarris std::string a = "std::cout << "Hello"<<std::endl;" is not using raw string literals. Did you read the link? Commented Sep 14, 2017 at 14:34
  • 1
    Resource-files or objcopy on Linux should do the trick. But there really isn't much you could do in a system-independent way apart from embedding the text in the source-file of your program. Commented Sep 14, 2017 at 14:35

1 Answer 1

4

If you're on Linux or similar, you can use objcopy to do this easily during your build process:

objcopy --input binary --output elf64-x86-64 myfile.cpp myfiletxt.o 

What this does is to create an object file called myfiletxt.o which you can then link into your executable. This object file will contain a symbol which is the entire content of myfile.cpp. You can then print it out etc.

Sign up to request clarification or add additional context in comments.

3 Comments

What's the name of the symbol? Can it be changed?
@Quentin: in this example the symbols are _binary_myfile_cpp_{start,end,size}. I think you can change them using objcopy --redefine-sym ....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.