c++g++

SQLite3 on C++ not worked


I want to use sqlite3 on my C++ project. But I have some problems. I download Zip-archive sqlite-amalgamation-3490100 and extract all files on /home/dima/Projects/C++. But this code doesn`t work.

#include <iostream>
#include <sqlite3.h>
#include <sqlite3ext.h>

using std::cout;
int main() {
    sqlite3 *db;
    int result  = sqlite3_open("test.db", &db);
    cout << "results = %d\n" << result;
    sqlite3_close(db);
    return 0;
}

My project is: main.cpp

When i compile main.cpp:

g++ main.cpp -Isqlite -o main

I have this error:

 In file included from main.cpp:3:
main.cpp: In function ‘int main()’:
sqlite/sqlite3ext.h:470:40: error: ‘sqlite3_api’ was not declared in this scope; did you mean ‘sqlite3_vfs’?
  470 | #define sqlite3_open                   sqlite3_api->open
      |                                        ^~~~~~~~~~~
main.cpp:9:19: note: in expansion of macro ‘sqlite3_open’
    9 |     int result  = sqlite3_open("test.db", &db);
      |                   ^~~~~~~~~~~~

Please, help me for sqlite3!


Solution

  • As mentioned, you should not include sqlite3ext.h and just have:

    #include <iostream>
    #include <sqlite3.h>
    
    using std::cout;
    int main()
    {
        sqlite3 *db;
        int result  = sqlite3_open("test.db", &db);
        cout << "result=" << result << "\n";
        sqlite3_close(db);
        return 0;
    }
    

    Then you can compile with:

    1. gcc -c sqlite3.c -o sqlite3.o
    2. g++ main.cpp sqlite3.o -I. -o main

    but as Pepijn Kramer noticed, you should have better to use a build system like cmake

    You have also an error in your std::cout statement since you try to use a format %d (coming from the printf history) but has no sense for std::cout.