Okay, so I went with a solution different from the ones suggested here. Here's what I did.
Inside my source code, I define a handler class where I will make use of the string literal I need. Two things are important here:
- The header file is actually an
.hpp.in while under version control. The implementation file is a normal .cpp
- Inside this header file, I choose a variable and #define it so:
#define MY_STRING @MY_STRING@
Now, inside the CMakeLists.txt responsible for these two files, I do this:
- Read the raw text file into a cmake variable
- Remove all
// comments, save the result in a temporary variable
- Replace all
\n characters with the string "newline", save the result into a new temporary variable
- Define the same variable I used in my header above above (i.e.
MY_STRING), and assign it from that latest temporary variable
- Copy the header from the its location under ${CMAKE_SOURCE_DIR} into the appropriate location under ${CMAKE_BINARY_DIR} (and renaming it to a
.hpp) by specifying @ONLY. This ensures that cmake will copy the header file, but will also update the #define MY_STRING @MY_STRING@ with the value of MY_STRING I set a little earlier.
Finally, inside the implementation file for my handler I define the following macros:
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
so that I can call TOSTRING(MY_STRING) and assign the output to an std::string my_string. Once this is done, all I need to do is replace the "newline" placeholder strings with "\n" - and I am done! my_string now has a value equivalent to the the one in the raw text file (sans the comments there).
I wish I had more time to play with this, and fewer other tasks to distract me, but the way things are, this will have to do for now.
Thank you all for you help!
std::string varname = "...";. The main thing to remember is to escape any double quotes and backslashes.#includestatement inside a raw string literal definition.