Home > Software engineering >  Error linking C code when including SQLite
Error linking C code when including SQLite

Time:09-19

On Visual Studio Code, Mac OS version 10 I am trying to compile (debug C/C file) a C code to connect to SQLite3 database. The compile ends with error :

ld: library not found for -lsqlite clang: error: linker command failed with exit code 1 (use -v to see invocation)

There is an SQLite3 folder under /usr/lib/ , which includes the SQLite3 library. sqlite3.h I put directly under the same source code file sqlite3test1.c. However the compile failed with an error. I link using this command on terminal :

/usr/bin/clang -lsqlite3 -fcolor-diagnostics -fansi-escape-codes -g /Users/Training/sqlite3test1.c -o /Users/Training/sqlite3test1 

The command generated an executable file, run correctly, and the executable file provided the database records on the terminal window ( Success ).

How to make the link without the command?

CodePudding user response:

SQLite is included in macOS and Mac OS X by default. So I think you are asking where to add your -lsqlite3 link parameter in the VSCode right? If so, there is a hidden folder .vscode, inside is a tasks.json:

Project Explorer on the Sidebar

Open task.json:

{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C  : clang build active file",
        "command": "/usr/bin/clang",
        "args": [
            "-fcolor-diagnostics",
            "-fansi-escape-codes",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    },
    {
        "type": "cppbuild",
        "label": "C/C  : clang build active file",
        "command": "/usr/bin/clang",
        "args": [
            "-fcolor-diagnostics",
            "-fansi-escape-codes",
            "-g",
            "-l",
            "sqlite3",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build",
        "detail": "compiler: /usr/bin/clang"
    }
],
"version": "2.0.0"
}

Did you find the sqlite3 command I added? That's it.

Now if you build and run the main.c file, VSCode will compile&link successfully, and you are ready to debug.

enter image description here

  • Related