I'm trying to compile a little C-program using SQLite3. I've already included the Header-File and converted the .dll file into a .lib file.
The interesting thing is that on windows, gcc (CodeBlocks) can compile the source code without any problems. But under Debian (Raspberry Pi) I'm getting this error message: /usr/bin/ld: cannot find -lsqlite3.lib
The sqlite3.lib file is in the same folder as the main.c file I want to compile. (I have also tried to copy the .lib file to /usr/bin/ - no success)
And if I try to run the windows-compiled program on my raspberry, I get another error message...
Here's my source code:
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
static int callback(void *NotUsed, int argc, char **argv, char **azColName);
int main(void){
sqlite3 *db=NULL;
int erg = 0;
char *errMsg = NULL;
erg = sqlite3_open("temp_values.db", &db);
if (erg == 1){
fprintf(stderr, "Fehler beim Oeffnen der DB!\n");
sqlite3_close(db);
return EXIT_FAILURE;
}
else fprintf(stdout, "Database connection successful!\n");
erg = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS temp_values (username TEXT, port TEXT, degrees INTEGER, humidity INTEGER, PRIMARY KEY(username, port));", callback, 0, &errMsg);
if (erg){
fprintf(stderr, "SQL error: %s\n", errMsg);
}
else fprintf(stdout, "Table check successful!\n");
erg = sqlite3_exec(db, "INSERT INTO temp_values (username, port, degrees, humidity) VALUES ('root', '4', 24, 35);", callback, 0, &errMsg);
if (erg){
fprintf(stderr, "SQL error: %s\n", errMsg);
}
else fprintf(stdout, "Inserted tuple successful!\n");
sqlite3_close(db);
return EXIT_SUCCESS;
}
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for (i = 0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
I hope someone can help me ...
To resolve this problem, you should either provide the library file ( lib{nameOfTheLibrary}. so ) in those search paths or use -L command option. -L{path} tells the g++ (actually ld ) to find library files in path {path} in addition to default paths.
LDFLAGS: Extra flags to give to compilers when they are supposed to invoke the linker, 'ld', such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead. LDLIBS: Library flags or names given to compilers when they are supposed to invoke the linker, 'ld'.
The most basic problem here is that you cannot take a binary file (a program, object file, library) that you have built on one operating system (e.g. Windows) and expect it work on another operating system (e.g. Linux). Binary file formats differ between operating systems.
Windows executables and libraries are unrecognizable to Debian. So if you
have a program that uses the sqlite3
library that you want to run on Debian,
you need to:
sqlite3
library that has also been built for Linux.A secondary problem is that, even if a Linux sqlite3
library was called sqlite3.lib
(which it wouldn't be), and even if sqlite3.lib
was in the current directory when
you tried to link your program, the linker option -lsqlite3.lib
will not enable
the linker to find it (as you have discovered).
The offical behaviour of the GNU linker option -l
foo
is to make the linker search a given directory in its library search paths for
a file called libfoo.a (a static library) or libfoo.so (a dynamic library) and
to prefer libfoo.so if it finds both in the same directory. Hence, -lsqlite3.lib
asks the linker to find either libsqlite3.lib.so
or libsqlite3.lib.a
, neither of
which exists.
On Windows, you will find that -lsqlite3.lib
works. That is because Windows
libraries do not follow the Linux convention that the foo static library
is libfoo.a and the foo dynamic library is libfoo.so. So the Windows port
of the GNU linker accepts Windows-style library names in the -l
option, as explained
here.
On Debian, the developer package of the sqlite3
library is available from the
package manager as libsqlite3-dev
. You can install it, as root, with the command:
sudo apt-get install libsqlite3-dev
After you have installed it, you can compile your program, say it is main.c
, with the command:
gcc -Wall -c -o main.o main.c
Link it:
gcc -o prog main.o -lsqlite3
Run it:
$ ./prog
Database connection successful!
Table check successful!
Inserted tuple successful!
issue this in a terminal
sudo apt-get install libsqlite3-dev
and window lib file does not work in linux
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With