【问题标题】:Can't Link to SQLite3 static library无法链接到 SQLite3 静态库
【发布时间】:2012-02-09 09:36:10
【问题描述】:

我有一个静态库 libsqlite3.a,我想链接到我的小程序。 我的make文件如下所示:

CPP = g++

sources = main.cpp
objects = main.o

included = -IC:/SQLite-lib/include
linked = -LC:/SQLite-lib/ -lsqlite3

main : $(objects)
    $(CPP) $(linked) $(objects) -o main

main.o : $(sources)
    $(CPP) $(included) -c main.cpp

我不断收到这种错误消息:

g++ -LC:/SQLite-lib/libsqlite3.a main.o -o main
main.o:main.cpp:(.text+0x42): undefined reference to `sqlite3_open'
main.o:main.cpp:(.text+0x7d): undefined reference to `sqlite3_close'
main.o:main.cpp:(.text+0xe7): undefined reference to `sqlite3_close'
collect2: ld returned 1 exit status
make: *** [main] Error 1

我做错了什么?我使用 Win XP SP3、GCC 4.6.2。

【问题讨论】:

  • makefile 和输出不匹配!

标签: c++ sqlite gcc linker makefile


【解决方案1】:

您必须始终将库放在您链接的文件之后。将makefile中的链接行更改为:

main : $(objects)
    $(CPP) $(objects) -o main $(linked)

应该可以的。

【讨论】:

    【解决方案2】:

    您的链接参数不正确。 -L 用于指定可以找到库的目录(搜索路径)。 -l 用于指定链接到哪些库。

    g++ -LC:/some/lib/path main.o -o main -lsqlite3
    

    【讨论】:

    • 我不知道,您的 Q 中的错误消息与您的 makefile 不再匹配,请更新它并保留原始 Makefile 和错误消息。此外,将$(CPP) 用于 C++ 编译器是一个坏主意。使用$(CXX)。 CPP 用于 C 预处理器,而不是 C++。 (并且 -lsqlite3 需要在 main.o 之后。)
    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多