-2

I'm using CMake and would like to compile a text file into an object file.

eg. I have a text file containing "hello\nworld\n". I would like this to compile to an object as if this file were in fact a C++ file:

char * myString = "hello\n\world\n";

Is there a simple way to do this, preferably in one step? Something like

text_target(myString file.txt);

genpfault
  • 51,148
  • 11
  • 85
  • 139
An Fuel
  • 67
  • 3
  • 1
    You can include the text file in your cpp file. I am not sure you can coerce CMake to treat `.txt` as a cpp extension. Also if the file contained just`"hello\nworld\n"` that would be an invalid c++ source file. – drescherjm Aug 17 '23 at 21:05
  • You could tell your *installer* program to package the text file with your executable, allowing you to use existing file I/O. – Thomas Matthews Aug 17 '23 at 23:24
  • Neither of those comments answers the question. However, the linked question does. – An Fuel Aug 20 '23 at 18:44
  • One point of comments is to ask the person asking the question to better clarify their problem. Your question was very unclear to me. I am surprised that @Tsyvarev guessed your problem correctly even though Tsyvarev is an expert in CMake related topics. – drescherjm Aug 20 '23 at 23:54
  • @drescherjm: Actually, I don't find the question difficult to understand: It clearly asks about embedding resources for use them in C++ code. While the wording "compile a text file into an object file" differs from the well-known *task* ("embedding resources"), it is quite good description of the *process*. Similarly, while requested representation of resources - a *null-terminated string* - is normally not very convenient (it cannot represent a content with a zero byte inside), that representation is very close the best one (array of bytes and its length). One more signpost to the common task – Tsyvarev Aug 21 '23 at 00:24

0 Answers0