在Linux上使用POSIX AIO lib编译C++程序

4 linux posix file aio

在编译在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
Run Code Online (Sandbox Code Playgroud)

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

Eva*_*ran 8

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

man aio_read

概要

   #include <aio.h>

   int aio_read(struct aiocb *aiocbp);

   Link with -lrt.
Run Code Online (Sandbox Code Playgroud)

所以你应该链接到这个:

g++ -lrt aio.cc -o aio
Run Code Online (Sandbox Code Playgroud)

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

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

-l自己添加一个库,如果文件名为libsomename.so,你只需使用"-lsomename"


小智 7

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

事实证明,-l稍后在gcc命令调用中放置标志(例如,最后一个)有时可以解决此问题.我偶然发现了这个解决方案在这里对堆栈溢出.

我不再这样做了:

gcc -Wall -Werror -g -o myExe -lrt myExe.c
Run Code Online (Sandbox Code Playgroud)

并开始这样做:

gcc -Wall -Werror -g -o myExe myExe.c -lrt
Run Code Online (Sandbox Code Playgroud)