简体   繁体   中英

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

I am attempting to use sqlite3 in a C++ project in Eclipse and have found a great deal of advice online on using the API, but unfortunately am falling at an earlier hurdle. I guess this is due to my lack of experience with C/C++ and CDT. I've simply copied sqlite3.c and sqlite3.h into the project's source folder and have a test method which is as follows:

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

However, the sqlite3.c file shows up in Eclipse with numerous errors. For example, the following section is annotated with 'Field 'IN_DECLARE_VTAB' could not 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 did find a similar question here , but it doesn't appear to have been resolved there either.

I suspect this is a set-up issue with Eclipse, so if anyone could give me any advice or directions to useful tutorials I'd really appreciate it. And if I'd be better off posting this to a dedicated sqlite forum just let me know.

Have you try in this way? (with double pointer):

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

I suppose you're working on linux.
Another approach is to execute a script:

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

Your file connectDB will be:

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

SQLite is written in C , and there are a number of differences between C and C++. Not huge numbers, but they're definitely not the same and neither is a superset of the other. Because you are using a single Eclipse project, you've probably ended up trying to compile C code with a C++ compiler, and are therefore coming unstuck on these small differences.

You are advised to build sqlite3.c into a separate library (it can be a static library or a dynamic one; your call) as a C project, and then make your C++ project just use that C project as a dependency. Or you can build it once and just have it as an external dependency; that'll work too. (To be fair, it's an external dependency; you shouldn't really embed it wholesale into your code anyway as that will make tracking bugfixes harder. Keeping it separate — at least for build, even if not for distribution — will make your life much easier.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM