1

I have a C++ program which dumps out a C++ program. Some of the functions are boiler plate code and certain functions has boiler plate code and code tailored based on a few variables.

A simplified example is presented below:

// Snippet: 1
#include <fstream>

using namespace std;
int main()
{
    ofstream fout("output.cc");

    // bp() has just boiler plate code
    fout << "void bp() {" << endl;
    fout << "std::cout << \"Hello World!\" << std::endl" << endl;
    // a few hundred lines of C++ code send to fout
    fout << "}" << endl;

    // mix() has boiler plate + some custom code
    int size = 4096;
    fout << "void mix() {" << endl;
    fout << "char buffer[" << size << "];" << endl;
    // a few hundred lines of C++ code send to fout
    fout << "}" << endl;

    // compile output.cc into *.so and delete output.cc

    return 0;
}

The output.cc gets compiled and user gets the *.so file. The user does not have access to output.cc.

I wanted to rewrite this since it is difficult to read the boiler plate code when it is inside fout and having escaped quotes makes it a nightmare. Hence I thought of storing the functions in a separate file. For example have bp() in bp.cc:

// file: bp.cc
void bp() {
    std::cout << "Hello World" << std::endl
    // a few hundred lines of C++ code 
}

Then the main file can be written as

int main()
{
    std::ifstream  src("bp.cc");
    std::ofstream  dst("output.cc");

    dst << src.rdbuf();
}

In case of mix() I would use the Form-Letter Programming by storing the function mix() in mix.cc.

When the functions bp() and mix() were dumped using fout as in Snippet:1, all I had to do was ship the executable since the Snippet:1 is self-contained. But

  • If I split the functions into separate files `bp()` into `bp.cc` and `mix()` into `mix.cc` how do I ship it as a single executable? I need to ship `bp.cc` and `mix.cc` along with the executable. I do not want the user to access `bp.cc` and `mix.cc`.
  • Is there a better way to rewrite the `Snippet:1` than what I have suggested to better suit my needs?
Anand
  • 1,122
  • 3
  • 20
  • 33
  • You might need to have something like a resource compiler that _bundles_ in the files with your executable. Another option might be to have another little tool, that generates all the _'horrible'_ escaping syntax for the original code templates given, and outputs a compilable c++ file with certain literals (e.g. `static const char[]`) for the templates. – πάντα ῥεῖ Feb 25 '14 at 21:21
  • @πάνταῥεῖ Post this as an answer? – anatolyg Feb 25 '14 at 22:10
  • Oh, nevermind - it's [here](http://stackoverflow.com/a/328137/509868) and [here](http://stackoverflow.com/a/411000/509868) – anatolyg Feb 25 '14 at 22:13
  • @anatolyg Thanks for feeding in the links. I didn't really want to make this an answer, there are just pointers ... – πάντα ῥεῖ Feb 25 '14 at 22:23
  • @anatolyg Hmm, is this a double duplicate then?? I think the [latter](http://stackoverflow.com/questions/410980/include-a-text-file-in-a-c-program-as-a-char/411000#411000) is the most concise answer to the problem. – πάντα ῥεῖ Feb 25 '14 at 22:29

1 Answers1

3

You can use raw string literals and just put the code into one of those:

#include <iostream>

char const source[] = R"end(
#include <iostream>
int main() {
    std::cout << "hello, world\n";
}
)end";

int main()
{
    std::cout << source;
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • We have not upgraded to C++11 yet. Is there a solution for the older versions? We do have access to Boost. – Anand Feb 25 '14 at 21:33