title: Assitance in statically linking a SQLite loadable extension in a C program compiled with the amalgamation
body: <p>I'm trying to compile SQLite with a C program along with at least one loadable non-core SQLite extension, as statically linked, expecting that to mean that there will be one executable file generated that will depend on no other files at that point.</p>
  <p>Right now, I'm trying to just get <code>carray.c</code> loaded. I have the <code>carray.c</code> file and the <code>carray.h</code> file. I've read the SQLite instructions for <a href="https://www.sqlite.org/loadext.html" rel="nofollow noreferrer">run-time loadable extensions</a>, especially item 6 for statically linking; but I really don't know enough to understand it properly.</p>
  <p>In item 6 of the instructions, would you please tell me the following?</p>
  <ol>
  <li>Where it reads, <code>To statically link your extension, simply add the -DSQLITE_CORE compile-time option</code> does this apply to compiling the <code>sqlite3.c</code> file, the <code>carray.c</code> file, or both? And how is this to be done?</li>
  </ol>
  <p>I should've said I'm using minGW-W64. I have been compiling the <code>sqlite3.c</code> amalgamation file with my C file by first generating a <code>sqlite3.o</code> file using, <code>gcc -O2 -c sqlite3.c -DSQLITE_ENABLE_JSON1</code> and then compiling that with my ever-changing code as <code>gcc -O2 sqlite3.o myfile.c -o myfile.exe</code>. And this has been working until trying to incorporate the <code>carray</code> loadable extension.</p>
  <p>Earlier I tried compiling <code>sqlite3.c</code> and <code>carray.c</code> each to their own <code>.o</code> files and then compiling them with <code>myfile.c</code> and it compiled without error; but carray was not recognized as a function when I tried to use it. I think it is the initializing/entry point item that is the problem.</p>
  <ol start="2">
  <li>I think I understand the name changing regarding <code>SQLITE_EXTENSION_INIT</code> and think I need to us <code>sqlite3_auto_extension()</code> because my C program will require multiple connections, not to the same database file but to multiple files. However, I get very confused at this point. When reading the <a href="https://www.sqlite.org/c3ref/auto_extension.html" rel="nofollow noreferrer">documentation</a> on <code>sqlite3_auto_extension()</code>, I've no idea what an entry point is or how to use/provide <code>xEntryPoint</code>. It also appears that the database handle is to be passed into <code>xEntryPoint</code> and that confuses me because I thought this was to automatically load the extension into every database connection not just for one handle.</li>
  </ol>
  <p>It also reads that <code>Then modify your application to invoke the entry point directly, passing in a NULL pointer as the third &quot;pApi&quot; parameter.</code> I think it reads elsewhere that this is to be done near the beginning of the main C function, but I don't follow how to do it.</p>
  <ol start="3">
  <li>Is the <code>carray.h</code> file to be included in my C program?</li>
  </ol>
  <p>This <a href="https://sqlite.org/forum/forumpost/6ad7d4f4be?t=h" rel="nofollow noreferrer">SQLite Forum question</a>, especially at post (3), regarding including the extension source in the amalgamation file, is interesting and I think I almost follow it; but don't understand how it compares to the regular instructions, or if it is a better method.</p>
  <ol start="4">
  <li>If possible, could you please explain what placing SQLITE_EXTENSION_INIT1 on a line by itself does or how compiling with -DSQLITE_CORE makes it a no-op?</li>
  </ol>
  <p>Thank you.</p>
  <hr />
  <p>The SQLite forum post mentioned above works. I just appended <code>carray.c</code> to the <code>sqlite3.c</code> amalgamation file, then appended the <code>core_init()</code> function example after it, changed one <code>sqlite3_auto_extension((void*)sqlite3_autobusy_init);</code> to use <code>sqlite3_carray_init</code>, and commented out the other since currently have just one extension to link, and included <code>carray.h</code> in my code. Then compiled the amalgamation as <code>gcc -O2 -c sqlite3.c -DSQLITE_ENABLE_JSON1 -DSQLITE_EXTRA_INIT=core_init</code>.  You don't need the first one with JSON1 but I left it in to make sure they'd work together. I'd still like to know how to do the same using the method in the instructions.</p>
  <p>Clarification on how to add the extension following the instructions, rather than appending to the amalgamation file, can be found here at this <a href="https://sqlite.org/forum/forumpost/f69e37b1a1?t=h" rel="nofollow noreferrer">SQLite Forum post</a>.</p>