Home > OS >  Error linking C code when including SQLite on Mac - clang compiler
Error linking C code when including SQLite on Mac - clang compiler

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 a sqlite3 folder under /usr/lib/ , which includes the sqlite3 library. also i got the sqlite3.h , i put directly under the same source code file sqlite3test1.c However the compile failed with Error, i could do the 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 ). now what do i need to do to make the link happens without the command ? Thanks for help!

CodePudding user response:

I'm also new to VSCode on macOS. The IDE is just powerful and easy to use.

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 it there is a tasks.json

Project Explorer on the Sidebar

Then if you edit this task.json, you will find the content below:

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

Cheers.

enter image description here

  • Related