How can I use sqlite3.c in a C ++ project?

I am trying to use sqlite3 in a C ++ project in Eclipse and have found a lot of advice on the Internet for using the API, but unfortunately I am attacking an earlier hurdle. I assume this is due to a lack of experience with C / C ++ and CDT. I just copied sqlite3.c and sqlite3.h to the project's source folder and got a test method that looks like this:

int main() { sqlite3* db; sqlite3** dbpointer = &db; const char* dbname = "test.db"; sqlite3_open(dbname, dbpointer); return 0; } 

However, the sqlite3.c file appears in Eclipse with numerous errors. For example, the next section annotates "Field" IN_DECLARE_VTAB "cannot be resolved."

 #ifdef SQLITE_OMIT_VIRTUALTABLE #define IN_DECLARE_VTAB 0 #else #define IN_DECLARE_VTAB (pParse->declareVtab) #endif 

When I try to compile, I get a series of errors like:

  gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/sqlite3.d" -MT"src/sqlite3.d" -o "src/sqlite3.o" "../src/sqlite3.c" ../src/sqlite3.c:30997: error: initializer element is not constant ../src/sqlite3.c:30997: error: (near initialization for `aSyscall[0].pCurrent') ../src/sqlite3.c:30997: error: initializer element is not constant ../src/sqlite3.c:30997: error: (near initialization for `aSyscall[0]') ../src/sqlite3.c:31009: error: initializer element is not constant ../src/sqlite3.c:31009: error: (near initialization for `aSyscall[1]') ../src/sqlite3.c:31017: error: initializer element is not constant ../src/sqlite3.c:31017: error: (near initialization for `aSyscall[2]') 

I found a similar question here , but it did not seem to be resolved either.

I suspect this is a problem with configuring Eclipse, so if anyone could give me any tips or directions on useful tutorials, I would really appreciate it. And if I would be better off posting this on a special sqlite forum, just let me know.

+4
source share
2 answers

Are you trying this way? (with double pointer):

 int main() { sqlite3* db; const char* dbname = "test.db"; sqlite3_open(dbname, &db); return 0; } 

I suppose you are working on Linux.
Another approach is to execute a script:

 int main() { system("connectDB.sh"); /* connectDB.sh should be chmod +x */ } 

Your connectDB file will be:

 #!/bin/bash sqlite3 test.db "select * from test.table" 
+1
source

SQLite is written in C , and there are a number of differences between C and C ++. Not huge numbers, but they are definitely not the same, and not one of them is a superset of the other. Since you are using the same Eclipse project, you probably tried to compile C code with the C ++ compiler and therefore put off these small differences.

It is recommended that you create sqlite3.c in a separate library (it can be a static library or dynamic, your call) as a C project, and then make your C ++ project just use this C project as a dependency. Or you can build it once and just have it as an external dependency; this will work too. (To be fair, this is an external dependency, you should not embed it in your code anyway, as this will greatly simplify tracking. Keeping it separate - at least for assembly, even if it is not for distribution, will significantly increase your life easier.)

+1
source

Source: https://habr.com/ru/post/1501175/


All Articles