【问题标题】:Compiling C++ program with POSIX AIO lib on Linux在 Linux 上使用 POSIX AIO lib 编译 C++ 程序
【发布时间】:2010-11-16 02:31:43
【问题描述】:

在 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 函数实际定义在哪里?

【问题讨论】:

  • 是的。我也试过 g++ -L /usr/lib64/libaio.a aio.cc -oaio 但得到了同样的链接器错误
  • 您没有链接到 aio 库,您只是将“/usr/lib/libaio.a”添加到库路径中。

标签: linux file posix aio


【解决方案1】:

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

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

我不再这样做了:

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

并开始这样做:

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

【讨论】:

    【解决方案2】:

    编辑:根据手册页,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”

    【讨论】:

    • 我已经进一步调查,我更新的答案应该有效。
    • 这对我不起作用,我仍然得到同样的错误。
    【解决方案3】:

    -L 指定搜索路径,-l 指定实际库吗?

    【讨论】:

    • 似乎 -L 指定了库文件的路径,而小写 l 使用 /etc/ld.so.conf 中指定的位置搜索库文件,至少在 Debian/Ubuntu 上。
    • 不,你弄错了。 -L 指定库的搜索路径(除了在 ld.so.conf 中找到的那些) -l 指定要链接到的库,不带 lib 前缀。你想要-laio
    【解决方案4】:

    您需要-laio 以链接到 libaio。 -o 的参数是您希望调用编译后的可执行文件。

    【讨论】:

      【解决方案5】:

      试试:

      sudo apt-get install libaio-dev
      

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

      【讨论】:

        【解决方案6】:

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

        谢谢,埃文。

        【讨论】:

          猜你喜欢
          • 2012-02-04
          • 2011-10-02
          • 2012-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多