2

Im sorry for the bad title. 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;
}
Pablochaches
  • 968
  • 10
  • 25
  • Then just quit mingw and use a proper ide like visual Studio. – Michael Chourdakis May 18 '20 at 03:30
  • Have you tried `g++ prueba.cpp -lsqlite3.dll` (note that this has nothing to do with the output of `dlltool`, it is linking the dll that you built in the first place) – lxop May 18 '20 at 03:33
  • @lxop `cannot find -lsqlite3.dll`, That was the result – Pablochaches May 18 '20 at 03:36
  • Don't you need `extern "C" { }` around the include (or whatever the syntax is)? – ikegami May 18 '20 at 03:48
  • Compiling and using libraries on windows is hard enough. I would just use the MSVC compiler. Visual Studio comes with it. You are not linking the libraries correctly and I don't know enough about mingw or windows to give you a satisfactory answer. – Bayleef May 18 '20 at 03:55
  • 2
    I always try to understand the reason for the massive amount of time people spend trying to cram Linux/Unix originated software, like gcc or sqlite, into an alien operating system that wasn't designed to run Linux-originated code. Even experienced developers will often have these kinds of difficulties, not to speak of people who are new to C++. Most of the time it will take much less time to simply install Linux from scratch, and get a fully working, fully configured, modern C++ compiler and libraries. It took me just an hour and a half, last time. How much time did you already spend on this? – Sam Varshavchik May 18 '20 at 04:00
  • 1
    @Sam because one usually get ill advice from the university to use mingw just because it's gcc and to use Linux on the stupid idea that it is better for developers. It's much more simple to install VS and ditch all useless stuff. The idea is that you need also a proper IDE, not just a compiler. And besides, unless one wants to create C++ server-side apps ( rare), Linux is completely useless even for learning the language. Most wanna be developers start and end with hello world until they realize that they have reached their limit. – Michael Chourdakis May 18 '20 at 05:08

2 Answers2

2

Bitten by this issue again today when working with g++.

Ensure the following:

  1. 'lib' prefix is part of the static link library name "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 ...
  2. Ensure Library search path is valid when using -L option
    ex: g++ -Lc:\static_libs -lsqlite3 -o app.exe main.cpp.
    In this case the library 'libsqlite3.lib' must be located as 'c:\static_libs\libsqlite3.lib'
  3. Or specify full path to the lib file without using -l or -L options
    ex: 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

mahee96
  • 705
  • 6
  • 15
1

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.

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40
  • It actually compiles, but it says that my pc can`t run the app. (Sorry if the translation is bad) – Pablochaches May 29 '20 at 13:35
  • Can you post the exact error message? If your compiler tells you in a different language than English you can set environment variable LANG=C to make sure the output is English. – Brecht Sanders May 29 '20 at 14:53
  • The problem is that it is not the compiler, it is windows that tells me that it canot run it. It gives no details – Pablochaches May 29 '20 at 17:44
  • Try opening the .exe file with Dependancy Walker https://www.dependencywalker.com/ It may be able to tell you why it's not a valid executable for your system (for example if it is a 64-bit Windows executable file while your running Windows 32-bit). Or you could try to run it with the gdb debugger. – Brecht Sanders May 29 '20 at 18:07
  • I know it's late but still, the error "your pc can't run the app" is because of option "-shared" which basically tells the g++ to build a shared library such as sqlite.so/sqlite3.dll but instead you want to use the .so/.dll and link edit it with your app which is ".exe" or executable. – mahee96 Nov 30 '22 at 17:57
  • You just need to use "g++ -o prueba.exe prueba.cpp -lsqlite3" instead of "g++ -shared -o prueba.exe prueba.cpp -lsqlite3" which basically creates dll/so named as exe – mahee96 Nov 30 '22 at 18:00
  • @mahee96 Not quite correct. When building an `.exe` the `-shared` flag means it should link with `.dll.a` shared libraries (which cause dependency on the matching `.dll` files) instead of `.a` static libraries. – Brecht Sanders Nov 30 '22 at 19:25
  • You might wanna recheck the documentation on -shared option for g++ – mahee96 Dec 01 '22 at 20:15
  • From Linux manpage for g++ https://linux.die.net/man/1/g++ -shared Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options that were used to generate code (-fpic, -fPIC, or model suboptions) when you specify this option. – mahee96 Dec 01 '22 at 20:15
  • https://www.herongyang.com/Linux-Apps/GCC-shared-to-Build-Dynamic-Library-Files.html https://stackoverflow.com/questions/23190606/creating-shared-library-through-g-how-to https://www.oreilly.com/library/view/c-cookbook/0596007612/ch01s05.html – mahee96 Dec 01 '22 at 20:16
  • I was explaining what the parameter does when linking `.exe` files. – Brecht Sanders Dec 02 '22 at 07:41