I have a file module.hpp
struct ModuleBase {
virtual void run() = 0;
};
and a main.cpp
program
int main() {
cout << ...?...; // here should go the contents of module.hpp
}
What can I put at ...?...
to let the contents of the header file printed here?
A basic idea would be
int main() {
static const string content = R"(
#include <module.hpp>
)";
cout << content;
}
but multi-line-strings are only available in C++11, and #include
does not work inside multi-line strings (which is good)?
If there is a non-portable way for the gcc... that would be a start.
Clarification (update): The substitution should be done at compile time.
The only real solution I know is to write a small program which converts a file into a C++ definition of a string variable containing it. This is fairly simple to write: output a simple header along the lines of:
char const variableName[] =
Then copy each line of the file, wrapping it in "...\n"
, and
escaping any characters necessary. (If you can be sure of
C++11, then you might be able to do something with R"..."
, but
I've no experience with this.)
[update: refering to the original question with a typo in it]:
Your solution should not work; if it does, it is an error in
the compiler. According to §2.2, tokenization occurs before
the execution of preprocessing directives. So when the
execution of preprocessing directives occurs, you have a string
literal, and not a #
preprocessing token. (Compiler errors
are to be expected when using C++11 features. There's not been
enough time yet for the implementers to get all of the bugs
out.)