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.
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.)
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"
Related
I'm starting a project using C++, which I haven't used before outside of a handful of school projects - nowhere near the scope of what I'm tackling now.
My goal is to try my best to follow the C++ Core Guidelines as I work to avoid errors, improve performance, and most importantly: improve maintainability of my code.
I've been running into literally hundreds of issues ranging from my g++ / Clang++ versions not being right to standard libraries not being found to g++ using the wrong version of C++ for compilation to very basic functions not behaving as expected - and I haven't even started to look into autotools, so I expect many more headaches to follow.
This question is specific to one part of the C++ Core Guidelines, though. Interfaces 6: Prefer Expects() for expressing preconditions
I tried writing the following simple code:
#include <iostream>
using namespace std;
int square(int x) {
Expects(x > 0);
return x * x;
}
int main() {
cout << square(3) << endl;
return 0;
}
This threw an error in g++:
$> g++ -std=c++17 main.cpp
main.cpp: In function ‘int square(int)’:
main.cpp:7:2: error: ‘Expects’ was not declared in this scope
Expects(x > 0);
^~~~~~~
-> [1]
I tried using Clang, as well, but it has an entirely different (and unrelated) problem:
$> clang++ -x c++ main.cpp
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
-> [1]
I haven't figured out how to fix that one yet, so I'm not bothering with it.
Expects is part of the GSL library. You have to use some GSL library implementation, which you can find on Github:
https://github.com/martinmoene/gsl-lite
https://github.com/Microsoft/GSL
These are the ones I have off the top of my head.
The CPP Guidelines likely allude to the "contracts" proposal which provides the same checks via attributes. It was scheduled for C++20, but later removed for lack of consensus on its scope. See p1823r0 and a standard committee member's Reddit thread on the rationale leading to the removal.
Apart from GSL, Excepts exists also in C++20 not in C++17 with a little different syntax
https://en.cppreference.com/w/cpp/language/attributes/contract
I am running Cygwin on windows 8, attempting to compile the source code for a game I would like to mod. Unfortunately I am running into some errors while building involving the fileno function. After doing some googling It seems like the problem might have to do with c++11 support (I'm not really sure what this means). Most of the solutions people have found involve adding some option like -std=c++0x or -std=c++11 when compiling, but my attempts to add the options into the makefile have been unsuccessful, and I don't know if that's whats causing the problem anyways. I'll include the code snippet that's throwing the error and a link to the makefile as it is quite large. Any advice you could give me would be great.
code that throws error:
time_t file_modtime(FILE *f)
{
struct stat filestat;
if (fstat(fileno(f), &filestat))
return 0;
return filestat.st_mtime;
}
Link to Makefile
it is being hosted on github
EDIT: After getting some advice I poked around the makefile and found five instances where the -std option was used, playing around with them hasn't changed anything. Is the problem with my Cygwin configuration? I installed the packages I was told I would need in the installation guide for the game I am building.
Changing the -std=c*** in your makefile to -std=gnu++0x should fix your problem.
If you don't know what c++11 is you're most likely not using it anyway.
Also if you need c++11 support you can also do: -std=gnu++11 instead of -std=gnu++0x
For windows...
fileno() is deprecated: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-fileno?view=vs-2017
use _fileno() instead: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fileno?view=vs-2017
I'm teaching myself C++ with the textbook 'Programming -- Principles and Practice Using C++ (Second Edition)' and have come across a problem while trying to run an example problem. The line I'm supposed to input is
constexpr int max = 17;
but I am getting an error: "Unable to resolve identifier constexpr", but I have no idea why because I have the necessary header file (specific to the textbook, downloaded from www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h ) All the other programs I've tried have worked just fine...
Other useful info:
-Using Netbeans (C++ Version)
-Using Windows 8
-Using Cygwin
-Not an expert.
constexpr is a new feature of C++11. You need a C++11 capable compiler (like g++4.8) and have to enable the C++11 extensions when you compile a program:
g++ -std=c++11 main.cpp -o test
I needed wanted to build "did you mean this?" feature to my website. I'm using sqlite3 and learned that i can use spellfix module to order tables via levenstein.
I downloaded source code of sqlite3 and compiled spellfix.c (it's inside /ext/misc/) like this:
gcc -shared -fPIC -Wall -I/tmp/sqlite-src-3071700/ spellfix.c -o spellfix
It compiles successfuly but when i load it into sqlite:
sqlite> .load ./spellfix
I'm getting this error:
Error: ./spellfix: undefined symbol: sqlite3_extension_init
I really have very few knowledge about compiling c programs. Did i do some mistake about compiling or something else is happened? What should i do?
It seems the sqlite init function is missing. There is a discussion here http://sqlite.1065341.n5.nabble.com/SQLite-version-3-7-16-td67776.html
I added the following code at the top.
static int spellfix1Register(sqlite3 *db);
int sqlite3_extension_init(sqlite3 *db, char ** pxErrMsg, const sqlite3_api_routines *pApi){
SQLITE_EXTENSION_INIT2(pApi);
return spellfix1Register(db);
}
Also needed the following since I couldn't pull in the headers for the sqlite3_stricmp function without creating additional problems:
int sqlite3_stricmp(const char *zLeft, const char *zRight){
return strcasecmp(zLeft, zRight);
}
And needed this too:
#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
Then it compiled and seemed to function correctly.
Is there any option to omit variable type or to set variable type to int in c++ code that to be compiled with g++ compiler in linux.
const bufLen = 2000;
Compilation went fine in solaris (as I am doing porting from solaris to linux).
One more thing ,I dont have control over the file as it is generated by some parser (provided by some third party in the form of binary)
Since I cant change the c++ file(as it is generated everytime before compilation) , I need some option (of g++) so I can include during compilation to suppress/resolve the error:
error: ISO C++ forbids declaration of `bufLen` with no type
EDIT :
INFO : options currently I am using -c -fPIC -Wno-deprecated -m32 -O2 -ffriend-injection -g
Is anyone of the options causing me trouble (or affecting other) ?
Thanks in advance
Thanks to AndersK who gave me a solution (through comments [below my question])
I tried compiling using -fms-extensions with g++ which resolved my problem
Reference : http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/C-Dialect-Options.html
PS : For visibility sake , I added the answer in answer section . Credits go to person who actually answered :)
Try -ansi or -std=c89. http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gcc/C-Dialect-Options.html