编译 C++ Linux 上使用 POSIX AIO lib 的程序
当我在 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 函数实际定义在哪里?
I'm having difficulty with the linker when it comes to compiling a sample program that uses the POSIX aio library (e.g. aio_read(), aio_write(), etc) on Linux.
I'm running Ubuntu with a 2.6 kernel, and have used the apt-get utility to install libaio. But even though I'm linking with the aio library, the compiler still gives me linker errors.
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
Where are all these aio_x functions actually defined, if not in the library libaio.a?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尽管正确安装了 aio 软件包并且存在
-lrt
标志,但我在链接libaio
时也遇到了问题。事实证明,在
gcc
命令调用中稍后(例如最后)放置-l
标志有时可以解决此问题。 我在 Stack Overflow 上此处偶然发现了这个解决方案。我停止这样做:
并开始这样做:
I also had issues linking against
libaio
in spite of the aio package being correctly installed and the-lrt
flag being present.It turned out that placing
-l
flags later (for example, last) in thegcc
command invocation sometimes fixes this issue. I stumbled upon this solution here on Stack Overflow.I stopped doing this:
And started doing this:
编辑:根据手册页,libaio.so不是链接到的正确库:
man aio_read
所以你应该链接到这个:
库与 gcc 的工作方式是这样的:
-L 将目录 dir 添加到要搜索 -l 的目录列表中。
-l 添加一个库本身,如果文件名为 libsomename.so,则只需使用“-lsomename”
EDIT: according the the man page, libaio.so is not the correct library to link to:
man aio_read
so you should link with this:
The way libraries work with gcc is like this:
-L adds directory dir to the list of directories to be searched for -l.
-l adds a library itself, if the file is named libsomename.so, you just use "-lsomename"
您需要
-laio
才能链接到 libaio。-o
的参数是您希望调用已编译的可执行文件的参数。You want
-laio
in order to link to libaio. The argument of-o
is what you want the compiled executable to be called.尝试:
然后确保在链接行上指定
-laio
。Try:
Then make sure you specify
-laio
on the link line.-L 指定搜索路径,-l 指定实际库吗?
Does -L specify the search path and -l specifies the actual library?
好吧,Evan Teran 是正确的 - 当我与 -lrt 链接时它起作用了。 看起来 aio_x 函数是在通用 POSIX 扩展库中定义的。
谢谢,埃文。
Okay, Evan Teran is correct - it worked when I linked with -lrt. It seems the aio_x functions are defined in a general POSIX extension library.
Thanks, Evan.