Reputation: 1068
I want to get started using sqlite with c++. So i downloaded the amalgamation from the site and compiled to get the .dll
gcc -shared sqlite3.c -o sqlite3.dll
I included the sqlite.h file in my project and the .dll file too. I compiled:
g++ prueba.cpp
and got this error message
C:\Users\PABLOS~1\AppData\Local\Temp\ccUI3YAt.o:prueba.cpp:(.text+0x2d): undefined reference to `sqlite3_open'
C:\Users\PABLOS~1\AppData\Local\Temp\ccUI3YAt.o:prueba.cpp:(.text+0x41): undefined reference to `sqlite3_errmsg'
collect2.exe: error: ld returned 1 exit status
Ok I said, lets see in stack overflow. In some question that I read they recomended to do this:
g++ main.cpp sqlite3.c
But the output was a really long list of error messages. I kept on reading but most of the questions where solved by:
sudo apt install libsqlite3-dev
or
gcc main.c -lsqlite3
In one of the questions the same guy that asked answered that he didnt include the .a file. So i googled about it and followed the instructions in this article. I created the .def file:
dlltool -z sqlite3.def --export-all-symbols sqlite3.dll
And created the .a file
dlltool -d sqlite3.def -l libsqlite3dll.a
Then included it in C:\MinGW\lib and tried again to compile
g++ prueba.cpp -lsqlite3dll
And i got the same error message. At this point im kind of lost (Im new to programing), and i dont know what to do next. Can you give me a pointer in the direction I should head in?
Edit: Answered a question form the coments
// This is my code
#include <iostream>
#include "sqlite3.h"
int main(int argc, char **argv) {
// Esto es lo que necesitamos para abrir la base de datos
sqlite3 *db;
char *zErrMsg = NULL;
int rc;
// La abrimos y revisamos por errores
rc = sqlite3_open("test.db", &db);
if (rc) {
std::cerr << "No se pudo abrir la base de datos: " << sqlite3_errmsg(db);
return 1;
}
std::cout << "Se pudo abrir la base de datos!"<< std::endl;
std::cin.get();
return 0;
}
Upvotes: 2
Views: 1371
Reputation: 21
I was trying to fix that problem for nearly two months and finally found a random video on YouTube. I just had to write "sqlite3.dll" when running the file, and add sqlite3.dll
to the cpp file directory, and it worked like magic. I do not even know what it changes, but it works. This is for future visitors facing the same problem
Add sqlite3.dll file to cpp file directory
When running the g++
command to run the file, add sqlite3.dll
at the end of command
g++ filename.cpp sqlite3.dll -o filename.exe
Upvotes: 2
Reputation: 842
Bitten by this issue again today when working with g++.
Ensure the following:
"libsqlite3.lib"
(my issue was that I was compiling sqlite3.def into sqlite3.lib and when passed to g++ using -l option it was failing to resolve function references during linking, the g++ compiler requires the library name to be prefixed with 'lib' as in libsqlite3.lib
and associated g++ command should be of the form: g++ -lsqlite3 ...
g++ -Lc:\static_libs -lsqlite3 -o app.exe main.cpp
.g++ -o app.exe main.cpp c:/static_export_libs/sqlite3.lib
In case you only have the precompiled shared library sqlite3.dll
and 'sqlite3.def', you can simply create exports file libsqlite3.exp
and its associated static link library libsqlite3.lib
using following sample command:
lib.exe" /machine:x64 /def:sqlite3_x64/sqlite3.def /out:sqlite3_x64/libsqlite3.lib
lib.exe" /machine:x86 /def:sqlite3_x86/sqlite3.def /out:sqlite3_x86/libsqlite3.lib
Upvotes: 2
Reputation: 7287
Instead of:
gcc -shared sqlite3.c -o sqlite3.dll
run:
gcc -shared sqlite3.c -o sqlite3.dll -Wl,--out-implib,libsqlite3.dll.a
This will give you both a .dll
binary and a .dll.a
library file.
Then instead of:
g++ prueba.cpp
run:
g++ -shared -o prueba.exe prueba.cpp -lsqlite3
If your .exe and .dll are in the same folder you should be able to run the .exe.
Upvotes: 1