9

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 ...

Michael Gierer
  • 401
  • 2
  • 6
  • 15
  • 2
    Do you have the sqlite-devel packages installed? On CentOS it's called sqlite-devel.x86_64 – Tim S. Apr 08 '16 at 19:55
  • 3
    If the error is really ` /usr/bin/ld: cannot find -lsqlite3.lib` I suspect problems in your makefile (linux static libs have a .a extension. – fvu Apr 08 '16 at 20:12
  • Please post your build recipe, makefile or whatever. In the question, not the comments, please. – Faheem Mitha Apr 08 '16 at 22:59
  • 1
    On Debian Jessie, you need `libsqlite3-dev`. – Faheem Mitha Apr 08 '16 at 23:01
  • 1
    If you try link windows `.lib` file with your code on linux, then this is expected result. You need sqlite compiled for OS/CPU. So you need three different instances of sqlite, for windows + x86, linux + x86 and linux + arm – fghj Apr 09 '16 at 03:46

2 Answers2

14

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:

  • Compile the source files on Debian to create Linux object files.
  • Link the object files with an 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 -lfoo 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!
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • Thank you very much! It worked with installing the libsqlite3-dev package! And next time I want to use libraries, I'll know how to do it (either .a or .so). You're guys are great! – Michael Gierer Apr 09 '16 at 14:19
  • @MichaelGierer Welcome to Stackoverflow! If this answer has solved your problem you can [accept it](http://stackoverflow.com/help/accepted-answer) by clicking the green check-mark. That's how you say "thanks". – Mike Kinghan Apr 09 '16 at 14:24
8

issue this in a terminal

sudo apt-get install libsqlite3-dev

and window lib file does not work in linux

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Jack Geller
  • 201
  • 1
  • 4