You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在VSCode中使用sqlite3库编译C代码失败(退出码-1)的排查与解决

问题分析与解决

项目结构

C:.
│   main.c
│
├───.dist
├───.vscode
│       c_cpp_properties.json
│       tasks.json
│
└───sqlite
        shell.c
        sqlite3.c
        sqlite3.def
        sqlite3.dll
        sqlite3.h
        sqlite3ext.h

main.c 代码

#include <stdio.h>
#include "sqlite/sqlite3.h"
#include "sqlite/sqlite3.c"

int main() {
    //printf("SQLite version: %s\n", sqlite3_version);
    //return 0;

    // Declaration of a SQLite database pointer
    sqlite3 *db;
    // Pointer to store SQLite error messages
    char *err_msg = 0;

    // Attempt to open the "Website_Project.db" database
    int rc = sqlite3_open("Website_Project.db", &db);

    // Check if the database opening was successful
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Unable to open the database: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // SQL query to create the "User" table if it doesn't exist
    char *sql = "CREATE TABLE IF NOT EXISTS User (\n"
                "                                         id INTEGER PRIMARY KEY,\n"
                "                                         username TEXT NOT NULL,\n"
                "                                         password TEXT NOT NULL\n"
                ");";

    // Execute the SQL query
    rc = sqlite3_exec(db, sql, 0, 0, &err_msg);

    // Check if the execution of the SQL query was successful
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL Error: %s\n", err_msg);

        // Free the memory allocated for the error message
        sqlite3_free(err_msg);

        // Close the database
        sqlite3_close(db);

        return rc;
    }

    // Close the database after a successful operation
    sqlite3_close(db);

    // Indication of the success of the program
    return 0;
}

问题原因

  1. 重复定义错误:直接在main.c中#include "sqlite/sqlite3.c"会将SQLite的所有函数实现嵌入到main.c编译单元中,后续编译若再包含sqlite3.c文件,会触发大量符号重复定义问题,这是程序崩溃的核心原因。
  2. 编译配置缺失:当前tasks.json未正确配置编译规则,既没有将sqlite3.c纳入编译流程,也未处理SQLite所需的链接依赖。

解决步骤

1. 修正main.c代码

移除对sqlite3.c的包含,仅保留头文件引用:

#include <stdio.h>
#include "sqlite/sqlite3.h"
// 删除 #include "sqlite/sqlite3.c"

2. 修改tasks.json配置

以下是针对MinGW编译器的tasks.json示例,将sqlite3.c加入编译流程:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "C:/MinGW/bin/gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "sqlite/sqlite3.c",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: C:/MinGW/bin/gcc.exe"
        }
    ]
}

若使用MSVC编译器,可参考如下tasks.json配置:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe 生成活动文件",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "sqlite\\sqlite3.c"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: cl.exe"
        }
    ]
}

3. 验证编译运行

修改完成后重新编译运行,程序应能正常创建Website_Project.db文件并生成User表,最终返回退出码0。

内容的提问来源于stack exchange,提问作者Shedar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 04:15:38