sqlite c ++体系结构x86_64的未定义符号

bil*_*eng 2 c++ sqlite macos

我正在Mac osx上运行sqlite c ++演示。我从下面显示的网页复制了代码。

参考:http : //www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm

源代码是

//     

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <fstream>
#include <string>

using namespace std;

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;
    char *sql;
    string str;

    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }else{
        fprintf(stdout, "Opened database successfully\n");
    }

    /* Create SQL statement */

    str = "CREATE TABLE location(country text, state text, city text );CREATE TABLE weather(temp real, tempunit text);";

    strcpy(sql,str.c_str());

    /* Execute SQL statement */
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }else{
        fprintf(stdout, "Table created successfully\n");
    }

    sqlite3_close(db);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

g++ sqlite.cpp  -v
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name sqlite.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 236.3 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/zerocraft/KuaiPan/Course/ECEN489/test -ferror-limit 19 -fmessage-length 166 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /var/folders/1d/dbmt5pd519bcp0dqr5vv76_c0000gn/T/sqlite-742d60.o -x c++ sqlite.cpp
clang -cc1 version 5.1 based upon LLVM 3.4svn default target x86_64-apple-darwin13.3.0
ignoring nonexistent directory "/usr/include/c++/v1"
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o a.out /var/folders/1d/dbmt5pd519bcp0dqr5vv76_c0000gn/T/sqlite-742d60.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_sqlite3_close", referenced from:
      _main in sqlite-742d60.o
  "_sqlite3_errmsg", referenced from:
      _main in sqlite-742d60.o
  "_sqlite3_exec", referenced from:
      _main in sqlite-742d60.o
  "_sqlite3_free", referenced from:
      _main in sqlite-742d60.o
  "_sqlite3_open", referenced from:
      _main in sqlite-742d60.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

我已经在笔记本电脑上安装了sqlite库。谁能对此有所启发?谢谢!

har*_*mic 5

You did not link against the sqlite library. The tutorial you were following said to do this:

g++ test.c -lsqlite3
Run Code Online (Sandbox Code Playgroud)

But you did this:

g++ sqlite.cpp  -v
Run Code Online (Sandbox Code Playgroud)

The option '-lsqlite3' tells the linker to link in the sqlite3 library. Without this, it cannot find the symbols that are defined in this library.