<p>I've got a C++ program that uses SQLite. I want to store the SQL queries in a separate file -- a plain-text file, <em>not</em> a source code file -- but embed that file in the executable file like a resource.</p>

<p>(This has to run on Linux, so I can't store it as an actual resource as far as I know, though that would be perfect if it were for Windows.)</p>

<p>Is there any simple way to do it, or will it effectively require me to write my own resource system for Linux? (Easily possible, but it would take a lot longer.)</p>

[{'answer_id': 72692, 'author': 'vava', 'author_id': 6258, 'author_profile': 'https://Stackoverflow.com/users/6258', 'pm_score': 2, 'selected': False, 'text': '<p>Use macros. Technically that file would be <em>source code</em> file but it wouldn\'t look like this.\nExample:</p>\n\n<pre><code>//queries.incl - SQL queries\nQ(SELECT * FROM Users)\nQ(INSERT [a] INTO Accounts)\n\n\n//source.cpp\n#define Q(query) #query,\nchar * queries[] = {\n#include "queries.incl"\n};\n#undef Q\n</code></pre>\n\n<p>Later on you could do all sorts of other processing on that file by the same file, say you\'d want to have array and a hash map of them, you could redefine Q to do another job and be done with it. </p>\n'}, {'answer_id': 72701, 'author': 'introp', 'author_id': 8398, 'author_profile': 'https://Stackoverflow.com/users/8398', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s slightly ugly, but you can always use something like:</p>\n\n<pre>const char *query_foo =\n#include "query_foo.txt"\n\nconst char *query_bar =\n#include "query_bar.txt"\n</pre>\n\n<p>Where query_foo.txt would contain the quoted query text.</p>\n'}, {'answer_id': 72714, 'author': 'Trent', 'author_id': 9083, 'author_profile': 'https://Stackoverflow.com/users/9083', 'pm_score': 2, 'selected': False, 'text': '<p>You can always write a small program or script to convert your text file into a header file and run it as part of your build process.</p>\n'}, {'answer_id': 72751, 'author': 'Matej', 'author_id': 11457, 'author_profile': 'https://Stackoverflow.com/users/11457', 'pm_score': 0, 'selected': False, 'text': '<p>I have seen this to be done by converting the resource file to a C source file with only one char array defined containing the content of resource file in a hexadecimal format (to avoid problems with malicious characters). This automatically generated source file is then simply compiled and linked to the project. </p>\n\n<p>It should be pretty easy to implement the convertor to dump C file for each resource file also as to write some facade functions for accessing the resources.</p>\n'}, {'answer_id': 72786, 'author': 'moonshadow', 'author_id': 11834, 'author_profile': 'https://Stackoverflow.com/users/11834', 'pm_score': 6, 'selected': True, 'text': '<p>You can use objcopy to bind the contents of the file to a symbol your program can use. See, for instance, <a href="http://www.linuxjournal.com/content/embedding-file-executable-aka-hello-world-version-5967" rel="noreferrer">here</a> for more information.</p>\n'}, {'answer_id': 73653, 'author': 'tfinniga', 'author_id': 9042, 'author_profile': 'https://Stackoverflow.com/users/9042', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s a sample that we used for cross-platform embeddeding of files.\nIt\'s pretty simplistic, but will probably work for you.</p>\n\n<p>You may also need to change how it\'s handling linefeeds in the escapeLine function.</p>\n\n<pre><code>#include &lt;string&gt;\n#include &lt;iostream&gt;\n#include &lt;fstream&gt;\n#include &lt;cstdio&gt;\n\nusing namespace std;\n\nstd::string escapeLine( std::string orig )\n{\n    string retme;\n    for (unsigned int i=0; i&lt;orig.size(); i++)\n    {\n        switch (orig[i])\n        {\n        case \'\\\\\':\n            retme += "\\\\\\\\";\n            break;\n        case \'"\':\n            retme += "\\\\\\"";\n            break;\n        case \'\\n\': // Strip out the final linefeed.\n            break;\n        default:\n            retme += orig[i];\n        }\n    }\n    retme += "\\\\n"; // Add an escaped linefeed to the escaped string.\n    return retme;\n}\n\nint main( int argc, char ** argv )\n{\n    string filenamein, filenameout;\n\n    if ( argc &gt; 1 )\n        filenamein = argv[ 1 ];\n    else\n    {\n        // Not enough arguments\n        fprintf( stderr, "Usage: %s &lt;file to convert.mel&gt; [ &lt;output file name.mel&gt; ]\\n", argv[0] );\n        exit( -1 );\n    }\n\n    if ( argc &gt; 2 )\n        filenameout = argv[ 2 ];\n    else\n    {\n        string new_ending = "_mel.h";\n        filenameout = filenamein;\n        std::string::size_type pos;\n        pos = filenameout.find( ".mel" );\n        if (pos == std::string::npos)\n            filenameout += new_ending;\n        else\n            filenameout.replace( pos, new_ending.size(), new_ending );\n    }\n\n    printf( "Converting \\"%s\\" to \\"%s\\"\\n", filenamein.c_str(), filenameout.c_str() );\n\n    ifstream filein( filenamein.c_str(), ios::in );\n    ofstream fileout( filenameout.c_str(), ios::out );\n\n    if (!filein.good())\n    {\n        fprintf( stderr, "Unable to open input file %s\\n", filenamein.c_str() );\n        exit( -2 );\n    }\n    if (!fileout.good())\n    {\n        fprintf( stderr, "Unable to open output file %s\\n", filenameout.c_str() );\n        exit( -3 );\n    }\n\n    // Write the file.\n    fileout &lt;&lt; "tempstr = ";\n\n    while( filein.good() )\n    {\n        string buff;\n        if ( getline( filein, buff ) )\n        {\n            fileout &lt;&lt; "\\"" &lt;&lt; escapeLine( buff ) &lt;&lt; "\\"" &lt;&lt; endl;\n        }\n    }\n\n    fileout &lt;&lt; ";" &lt;&lt; endl;\n\n    filein.close();\n    fileout.close();\n\n    return 0;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/72616', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12193/']