0

I followed the 'C/C++ for Visual Studio Code' official tutorial https://code.visualstudio.com/docs/languages/cpp and was able to compile a Hello World.

However now that I'm trying to compile this, the compile fails:

'banking.cpp'

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

int main(int argc, char** argv)
{
    sqlite3* DB;
    int exit = 0;
    exit = sqlite3_open("banking.db", &DB);

    if(exit)
    {
        std::cerr << "Error opening DB" << sqlite3_errmsg(DB) << std::endl;
    }
    else
    {
        std::cout << "Opened Datadase Successfully!" << std::endl;
        sqlite3_close(DB);
    }
    return (0);
}

Error Message:

C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\locti\OneDrive\Documents\cpp_projects\banking_cpp\banking.cpp -o C:\Users\locti\OneDrive\Documents\cpp_projects\banking_cpp\banking.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\locti\AppData\Local\Temp\ccHQC2y3.o:C:\Users\locti\OneDrive\Documents\cpp_projects\banking_cpp/banking.cpp:23: undefined reference to `sqlite3_open'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\locti\AppData\Local\Temp\ccHQC2y3.o: in function `main':
C:\Users\locti\OneDrive\Documents\cpp_projects\banking_cpp/banking.cpp:27: undefined reference to `sqlite3_errmsg'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\locti\OneDrive\Documents\cpp_projects\banking_cpp/banking.cpp:32: undefined reference to `sqlite3_close'
collect2.exe: error: ld returned 1 exit status

Here is the folder structure:

/banking_cpp
|/.vscode
|/sqlite
||shell.c
||sqlite3.c
||sqlite3.h
||sqlite3ext.h
|banking.cpp

Werid thing is that vs code managed to create a banking.exe before I restarted my computer but that exe couldnt be debugged and gave file format not recognized error

asd417
  • 1
  • 1
  • Show how you are building. I expect you are only building your banking.cpp file. Did you edit your tasks.json to have it build more than 1 file? – drescherjm Apr 20 '22 at 03:00
  • Does this answer your question? [Error: undefined reference to \`sqlite3\_open'](https://stackoverflow.com/questions/9389344/error-undefined-reference-to-sqlite3-open) – Darth-CodeX Apr 20 '22 at 03:14
  • I havent edited tasks.json. Do i have to specify all the files I'm compiling? – asd417 Apr 20 '22 at 03:16
  • I saw that post but isnt that for compiling with library rather than source code? I want to be able to jsut git this project without dealing with linkers or compile settings – asd417 Apr 20 '22 at 03:22
  • 1
    You have to link in whatever you need. If you provide the source file you must link with the object file created from that source. If you provide library, you must link with that library. The compiler and linker options are just different but won't simply disappear. – Gerhardh Apr 20 '22 at 05:38
  • You need to edit your tasks.json and have it build `banking.cpp`,`sqlite3.c` and `shell.c` by default it will build only the active file into your executable. – drescherjm Apr 20 '22 at 09:59

0 Answers0