简体   繁体   English

如何在C ++项目中使用sqlite3.c?

[英]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. 我正在尝试在Eclipse的C ++项目中使用sqlite3,并且在网上找到了大量有关使用API​​的建议,但不幸的是,这是一个较早的障碍。 I guess this is due to my lack of experience with C/C++ and CDT. 我想这是由于我缺乏C / C ++和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: 我仅将sqlite3.c和sqlite3.h复制到项目的源文件夹中,并具有如下测试方法:

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. 但是,sqlite3.c文件在Eclipse中显示,但出现许多错误。 For example, the following section is annotated with 'Field 'IN_DECLARE_VTAB' could not be resolved'. 例如,以下部分用“无法解析的字段'IN_DECLARE_VTAB'”进行了注释。

#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. 我怀疑这是Eclipse的设置问题,因此,如果有人可以给我任何建议或指导,以帮助您了解有用的教程,我将不胜感激。 And if I'd be better off posting this to a dedicated sqlite forum just let me know. 如果我最好将其发布到专门的sqlite论坛,请告诉我。

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. 我想您正在Linux上工作。
Another approach is to execute a script: 另一种方法是执行脚本:

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

Your file connectDB will be: 您的文件connectDB将为:

#!/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++. SQLite用C编写,C和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. 因为您使用的是单个Eclipse项目,所以您可能最终尝试使用C ++编译器来编译C代码,因此在这些小的差异上没有被发现。

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. 建议您将sqlite3.c作为C项目构建到一个单独的库(可以是静态库或动态库;您可以调用)中,然后使您的C ++项目仅使用该C项目作为依赖项。 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.) (为公平起见,这是一个外部依赖关系;您无论如何都不应将其完全嵌入到您的代码中,因为那样会使跟踪错误修正更加困难。将其分开(至少用于构建,即使不是用于分发)也会使您的生活变得更加艰辛。更轻松。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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