4

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?

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • Yes. I also tried g++ -L /usr/lib64/libaio.a aio.cc -oaio but got the same linker error –  Aug 01 '09 at 23:17
  • you aren't linking with the aio library, you simply added "/usr/lib/libaio.a" to the library path. – Evan Teran Aug 01 '09 at 23:26

6 Answers6

11

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 the gcc command invocation sometimes fixes this issue. I stumbled upon this solution here on Stack Overflow.

I stopped doing this:

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

And started doing this:

gcc -Wall -Werror -g -o myExe myExe.c -lrt
Community
  • 1
  • 1
Andre
  • 301
  • 2
  • 6
9

EDIT: according the the man page, libaio.so is not the correct library to link to:

man aio_read

SYNOPSIS

   #include <aio.h>

   int aio_read(struct aiocb *aiocbp);

   Link with -lrt.

so you should link with this:

g++ -lrt aio.cc -o aio

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"

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
1

Try:

sudo apt-get install libaio-dev

Then make sure you specify -laio on the link line.

Jared Oberhaus
  • 14,547
  • 4
  • 56
  • 55
1

You want -laio in order to link to libaio. The argument of -o is what you want the compiled executable to be called.

Meredith L. Patterson
  • 4,853
  • 29
  • 30
0

Does -L specify the search path and -l specifies the actual library?

Karl Voigtland
  • 7,637
  • 34
  • 29
  • It seems that -L specifies the path of the library file, whereas lowercase l searches for the library file using the locations specified in /etc/ld.so.conf, at least on Debian/Ubuntu. –  Aug 01 '09 at 23:26
  • 1
    No, you got it in the wrong order. -L specifies search paths for libraries(in addition to those found in ld.so.conf) -l specifies a library to link to without the lib prefix. You want -laio – nos Aug 02 '09 at 01:20
0

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.