使用cl.exe编译sqlite示例代码时出现链接器2019错误

时间:2016-04-26 21:48:50

标签: c

我知道这是某种链接错误,可能是因为我错过了某种编译器选项。所以,请让我知道我做错了什么。

我的代码来自示例程序@ http://www.sqlite.org/quickstart.html

#include <stdio.h>
#include <sqlite3.h>

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;
    }

int main(int argc, char **argv){
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    if( argc!=3 ){
        fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
        return(1);
        }
    rc = sqlite3_open(argv[1], &db);
    if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return(1);
        }
    rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
    if( rc!=SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        }
    sqlite3_close(db);
    return 0;
    }

我正在使用&#34;开发人员命令提示符&#34;在Windows中使用以下命令进行编译:( SQLite头文件位于%USERPROFILE%\ lib中)

cl /I%USERPROFILE%\lib helloworld.c

我收到以下错误消息:

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23918 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

helloworld.c
Microsoft (R) Incremental Linker Version 14.00.23918.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:helloworld.exe
helloworld.obj
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_close referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_exec referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_free referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_open referenced in function _main
helloworld.obj : error LNK2019: unresolved external symbol _sqlite3_errmsg referenced in function _main
helloworld.exe : fatal error LNK1120: 5 unresolved externals

1 个答案:

答案 0 :(得分:1)

在命令行中添加sqlite库文件,如下所示:

cl /I%USERPROFILE%\lib helloworld.c sqlite3.lib
编辑:看起来他们在预编译的二进制文件中提供.def文件,而没有.lib。要创建.lib:

lib /def:sqlite3.def /OUT:sqlite3.lib