Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC Compile Error: /usr/bin/ld: cannot find -lsqlite3.lib

Tags:

c

gcc

libraries

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

like image 986
Michael Gierer Avatar asked Apr 08 '16 19:04

Michael Gierer


People also ask

How do you resolve usr bin Ld Cannot find?

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.

What is Ldflags in Makefile?

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


2 Answers

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!
like image 193
Mike Kinghan Avatar answered Oct 04 '22 19:10

Mike Kinghan


issue this in a terminal

sudo apt-get install libsqlite3-dev

and window lib file does not work in linux

like image 21
Jack Geller Avatar answered Oct 04 '22 18:10

Jack Geller