SQLite Forum

Enabling JSON1 extension in the sqlite3.c amalgamation at compile time
Login

Enabling JSON1 extension in the sqlite3.c amalgamation at compile time

(1) By Gary (1codedebugger) on 2020-11-02 16:59:46 [link] [source]

I'm sure this is a very basic/stupid question but I'm having trouble getting the JSON1 extension to work in the sqlite3.c amalgamation file.

I'm using minGW-W64 and tried gcc -DSQLITE_ENABLE_JSON1 -c sqlite3.c and generated the sqlite3.o file without error but when compile that with my code using gcc sqlite3.o myfile.c -o myfile.exe, the JSON1 functions are still not recognized.

Where the compiling instructions read that the extension is included in the amalgamation file but disabled by default, is there more to enabling it than this?

I'm just experimenting at the moment and expect that I will need to use more compile options for the final files, but I can't even get this simple thing to work for I don't know enough about compiling.

Thank you.

(2) By anonymous on 2020-11-03 15:25:57 in reply to 1 [link] [source]

Your first step should be to test if the extension is really activated.
So run a test with only the preprocessor, something like:

gcc -E sqlite3.c -DSQLITE_ENABLE_JSON1 >sqlite3.txt

and check the output for the JSON1 functionality.
If the JSON1 functions are not recognised, are there any specific error messages or codes?

(3) By Warren Young (wyoung) on 2020-11-03 18:07:49 in reply to 1 [link] [source]

Are you certain you're linking to the new sqlite3.o file, not a prior one?

(4) By Gary (1codedebugger) on 2020-11-03 23:33:39 in reply to 2 [link] [source]

Thank you for responding to my question.  I tried as you suggested and the statement you provided processed without error.  In the sqlite3.txt file generated, it appears that the extension is enabled and there are JSON functions in it. Does this mean it is enabled?  Thank you.


# 1 "sqlite3.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "sqlite3.c"
# 72 "sqlite3.c"
static const char * const sqlite3azCompileOpt[] = {
# 111 "sqlite3.c"
  "COMPILER=gcc-" "8.1.0",
# 279 "sqlite3.c"
  "ENABLE_JSON1",
# 748 "sqlite3.c"
  "THREADSAFE=1",
# 780 "sqlite3.c"
};

One function for example.

# 187964 "sqlite3.c"
static int sqlite3Json1Init(sqlite3 *db){
  int rc = 0;
  unsigned int i;
  static const struct {
     const char *zName;
     int nArg;
     int flag;
     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  } aFunc[] = {
    { "json", 1, 0, jsonRemoveFunc },
    { "json_array", -1, 0, jsonArrayFunc },
    { "json_array_length", 1, 0, jsonArrayLengthFunc },
    { "json_array_length", 2, 0, jsonArrayLengthFunc },
    { "json_extract", -1, 0, jsonExtractFunc },
    { "json_insert", -1, 0, jsonSetFunc },
    { "json_object", -1, 0, jsonObjectFunc },
    { "json_patch", 2, 0, jsonPatchFunc },
    { "json_quote", 1, 0, jsonQuoteFunc },
    { "json_remove", -1, 0, jsonRemoveFunc },
    { "json_replace", -1, 0, jsonReplaceFunc },
    { "json_set", -1, 1, jsonSetFunc },
    { "json_type", 1, 0, jsonTypeFunc },
    { "json_type", 2, 0, jsonTypeFunc },
    { "json_valid", 1, 0, jsonValidFunc },
  };

(5) By Gary (1codedebugger) on 2020-11-03 23:41:15 in reply to 3 [link] [source]

Thank you for responding to my question. That is good place to start. I deleted the sqlite3.o file and compiled it again and the result is the same; the json functions are still not recognized.

Unless I am misunderstanding. A warning is returned of implicit declaration of function 'json_extract', which I took to mean the extension was not enabled.

Thank you.

(6) By Keith Medcalf (kmedcalf) on 2020-11-04 05:13:05 in reply to 5 [link] [source]

No, that message means that the function was implicitly defined (used) before it was defined (declared).

In other words, the message means exactly and precisely what it says.

This means, for example, that you have code which calls a function before it has been defined.

You have provided insufficient information for diagnosis.

In other words, you have not provided sufficient information about WHEN you received this warning message nor WHERE you received this message.

This is a "COMPILE TIME" error. If the function did not exist at link time you would get a message that the function could not be found and no executable would be produced.

(7) By Gary (1codedebugger) on 2020-11-04 06:15:51 in reply to 6 [link] [source]

Thank you. I apologize if my terms were not sufficiently precise where I wrote that I took the error "to mean" that the extension was not enabled. I should have written that I took the error "to be the result of" the extension having not been enabled. I know what the error means in itself but I don't understand why it is being returned.

I've assumed that enabling the extension also declares the functions for subsequent use in my code and, therefore, am asking whether or not the error is the result of the extension having not been enabled.

Am I misunderstanding that? Should not enabling the extension also declare the functions for use? Have I placed the files in the wrong order, such that the functions are being invoked before they have been declared?

The WHEN and WHERE are:

I'm using minGW-W64 and at the run terminal I tried gcc -DSQLITE_ENABLE_JSON1 -c sqlite3.c and generated the sqlite3.o file without error; but, when that is compiled/linked with my code using gcc sqlite3.o myfile.c -o myfile.exe, the error I mentioned above is returned and the executable is not generated.

I tried what was suggeested above at the run terminal, which is gcc -E sqlite3.c -DSQLITE_ENABLE_JSON1 >sqlite3.txt and the result in that file, starting at the statement

static const char * const sqlite3azCompileOpt[] = { # 111 "sqlite3.c" "COMPILER=gcc-" "8.1.0", # 279 "sqlite3.c" "ENABLE_JSON1", # 748 "sqlite3.c" "THREADSAFE=1", # 780 "sqlite3.c" };

appears to indicate that the request to enable the extension was recognized; but, for some reason, almost certainly something I am doing wrong, the functions are not being declared.

Thank you for your assistance.

(8) By Keith Medcalf (kmedcalf) on 2020-11-04 08:24:28 in reply to 7 [link] [source]

I've assumed that enabling the extension also declares the functions for subsequent use in my code

Declaring/enabling the functions makes them accessible for use by SQLite3 in SQL statements which you send to SQLite3 via its API for execution. Extension functions are not for your use.

So, how are you calling the function?

(9) By Gary (1codedebugger) on 2020-11-04 15:32:47 in reply to 8 [source]

Oh, I see. Thank you.

That would be what I was doing wrong. I tried to invoke json_extract on a test string at a normal line of C code not within a SQLite3 API to confirm that the extension had been enabled.

I figured I had to have been doing something stupid. I thought the extension could be used for both purposes and was going to use it to break JSON strings passed to the C application into components before inserting/updating the database.

Thanks again for the explanation.

Since my question really isn't about enabling the extension after all, and is just my misunderstanding of how it is to be used, should I do something to remove the question or should it remain for others who might also make the same mistake?

(10) By Stephan Beal (stephan) on 2020-11-04 15:42:28 in reply to 9 [link] [source]

should I do something to remove the question or should it remain for others who might also make the same mistake?

Leave it intact, please. Deleting individual posts leaves holes in the conversation (the responses are not deleted), causing more confusion than is normally justified.