cygwin 中的 SQLite3 导致链接器错误

发布于 2024-11-02 12:53:31 字数 1334 浏览 0 评论 0原文

我正在尝试在 cygwin 中运行一个基本的“hello world”sqlite3 程序。我最初是通过 setup.exe 安装的,但是当我运行该程序时,我收到了链接器错误,例如“对 `_sqlite3_open' 的未定义引用”。我在编译时使用 -lsqlite3 开关。

位置在这里:

$ ls /usr/lib/ | grep sql
libsqlite3.a
libsqlite3.dll.a
libsqlite3.la

然后我尝试下载 sqlite3 源并手动编译它,但我得到了相同的错误。它安装到 /usr/local/lib

$ ls /usr/local/lib/ | grep sql
libsqlite3.a
libsqlite3.dll.a
libsqlite3.la

但我仍然收到这些链接器错误,而且我似乎不知道如何修复它?

$ gcc -lsqlite3 test.cc
test.cc: In function ‘int main()’:
test.cc:41: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccKA3ZFa.o:test.cc:(.text+0x2d): undefined reference to `_sqlite3_open'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x41): undefined reference to `_sqlite3_errmsg'
/tmp/ccKA3ZFa.o:test.cc:(.text+0xb6): undefined reference to `_sqlite3_prepare_v2'
/tmp/ccKA3ZFa.o:test.cc:(.text+0xca): undefined reference to `_sqlite3_errmsg'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x10e): undefined reference to `_sqlite3_step'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x12a): undefined reference to `_sqlite3_column_bytes'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x140): undefined reference to `_sqlite3_column_text'
/tmp/ccKA3ZFa.o:test.cc:(.eh_frame+0x11): undefined reference to `___gxx_personality_v0'
collect2: ld returned 1 exit status

感谢您提供的任何帮助。干杯:)

I'm trying to get a basic "hello world" sqlite3 program going in cygwin. I had originally installed it from the setup.exe, but when I ran the program I got linker errors such as "undefined reference to `_sqlite3_open'". I AM using the -lsqlite3 switch when compiling.

The location is here:

$ ls /usr/lib/ | grep sql
libsqlite3.a
libsqlite3.dll.a
libsqlite3.la

Then I tried downloading the sqlite3 source and compiling it manually, but i get the same errors. It installed to /usr/local/lib

$ ls /usr/local/lib/ | grep sql
libsqlite3.a
libsqlite3.dll.a
libsqlite3.la

I'm still getting these linker errors though, and I can't seem to figure out how to fix it?

$ gcc -lsqlite3 test.cc
test.cc: In function ‘int main()’:
test.cc:41: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccKA3ZFa.o:test.cc:(.text+0x2d): undefined reference to `_sqlite3_open'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x41): undefined reference to `_sqlite3_errmsg'
/tmp/ccKA3ZFa.o:test.cc:(.text+0xb6): undefined reference to `_sqlite3_prepare_v2'
/tmp/ccKA3ZFa.o:test.cc:(.text+0xca): undefined reference to `_sqlite3_errmsg'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x10e): undefined reference to `_sqlite3_step'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x12a): undefined reference to `_sqlite3_column_bytes'
/tmp/ccKA3ZFa.o:test.cc:(.text+0x140): undefined reference to `_sqlite3_column_text'
/tmp/ccKA3ZFa.o:test.cc:(.eh_frame+0x11): undefined reference to `___gxx_personality_v0'
collect2: ld returned 1 exit status

Thanks for any help you can provide. Cheers :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-11-09 12:53:31

这是导致您出现问题的 -l 选项的位置。尝试执行此操作:

gcc test.cc -lsqlite3

按照以下记录:

pax$ cat qq.c
#include <sqlite3.h>
int main (void) {
        sqlite3 *x;
        sqlite3_open("db", &x);
        return 0;
}

pax$ gcc -L/usr/lib -lsqlite3 -o qq qq.c
/cygdrive/c/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccmgVbDt.o:
    qq.c:(.text+0x25): undefined reference to `_sqlite3_open'
collect2: ld returned 1 exit status

pax$ gcc -L/usr/lib -o qq qq.c -lsqlite3

pax$ _

您可以看到,当 -l 跟随其他参数时,不存在链接器错误。

手册页中的相关位是(在 -l 的描述下):

在命令中写入此选项的位置会有所不同;链接器搜索并
按照指定的顺序处理库和目标文件。因此,foo.o -lz bar.o 在文件 foo.o 之后、bar.o 之前搜索库 z >。如果 bar.o 引用 z 中的函数,则可能不会加载这些函数。

换句话说,在您指定 -lsqlite3 时,没有对任何 SQLite 函数的未解析引用,因此它们都不会被加载。

稍后,当您加载 test.o 时,它确实有未解析的引用。不幸的是,由于您已经查看了 SQLite3 库并对其打了折扣,因此它们永远不会得到解决。因此你的错误。

It's the placement of the -l option which is causing your problem. Try executing this instead:

gcc test.cc -lsqlite3

As per the following transcript:

pax$ cat qq.c
#include <sqlite3.h>
int main (void) {
        sqlite3 *x;
        sqlite3_open("db", &x);
        return 0;
}

pax$ gcc -L/usr/lib -lsqlite3 -o qq qq.c
/cygdrive/c/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccmgVbDt.o:
    qq.c:(.text+0x25): undefined reference to `_sqlite3_open'
collect2: ld returned 1 exit status

pax$ gcc -L/usr/lib -o qq qq.c -lsqlite3

pax$ _

You can see that, when the -l follows the other arguments, there is no linker error.

The relevant bit from the man page is (under the description of -l):

It makes a difference where in the command you write this option; the linker searches and
processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

In other words, at the point where you specify -lsqlite3, there are no unresolved references to any of the SQLite functions so none of them are loaded.

Later on, when you load test.o, it does have unresolved references. Unfortunately, since you've already looked at the SQLite3 library and discounted it, they will never be resolved. Hence your error.

走野 2024-11-09 12:53:31

您可能在命令末尾需要您的库:

gcc test.cc -lsqlite3

链接器通常从左到右搜索符号,并且 SQLite 符号在 test.cc 中引用,但在 libsqlite3 中定义。一个。

You probably need your library at the end of the command:

gcc test.cc -lsqlite3

The linker usually searches for symbols from the left to the right and the SQLite symbols are being referenced in test.cc but defined in libsqlite3.a.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文