4

在 Linux 上编译使用 POSIX aio 库(例如 aio_read()、aio_write() 等)的示例程序时,我遇到了链接器的困难。

我正在运行带有 2.6 内核的 Ubuntu,并使用了 apt-get 实用程序来安装 libaio。但即使我正在链接 aio 库,编译器仍然会给我链接器错误。

root@ubuntu:/home# g++ -L /usr/lib/libaio.a aio.cc -oaio
/tmp/cc5OE58r.o: In function `main':
aio.cc:(.text+0x156): undefined reference to `aio_read'
aio.cc:(.text+0x17b): undefined reference to `aio_error'
aio.cc:(.text+0x191): undefined reference to `aio_return'
collect2: ld returned 1 exit status

如果不在库 libaio.a 中,所有这些 aio_x 函数实际定义在哪里?

4

6 回答 6

11

libaio尽管正确安装了 aio 包并且存在标志,但我也遇到了链接问题-lrt

事实证明,在命令调用-l中稍后(例如,最后)放置标志有时可以解决此问题。我在 Stack Overflow 上gcc偶然发现了这个解决方案

我停止这样做:

gcc -Wall -Werror -g -o myExe -lrt myExe.c

并开始这样做:

gcc -Wall -Werror -g -o myExe myExe.c -lrt
于 2014-12-15T14:10:24.507 回答
8

编辑:根据手册页, libaio.so 不是要链接到的正确库:

man aio_read

概要

   #include <aio.h>

   int aio_read(struct aiocb *aiocbp);

   Link with -lrt.

所以你应该链接这个:

g++ -lrt aio.cc -o aio

库使用 gcc 的方式是这样的:

-L 将目录 dir 添加到要搜索的目录列表中 -l。

-l 添加一个库本身,如果文件名为 libsomename.so,则只需使用“-lsomename”

于 2009-08-01T23:24:22.990 回答
0

尝试:

sudo apt-get install libaio-dev

然后确保-laio在链接行上指定。

于 2009-08-01T23:26:16.543 回答
0

您想要-laio链接到 libaio。的参数-o是您希望调用已编译的可执行文件的内容。

于 2009-08-01T23:26:40.023 回答
0

好的,Evan Teran 是正确的 - 当我与 -lrt 链接时它起作用了。似乎 aio_x 函数是在通用 POSIX 扩展库中定义的。

谢谢,埃文。

于 2009-08-02T17:21:23.600 回答
0

-L是否指定搜索路径而-l是否指定实际库?

于 2009-08-01T23:20:52.483 回答