views:

689

answers:

5

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?

A: 

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

Karl Voigtland
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.
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
+2  A: 

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
That also returns the same linker error.
I have looked into things further, my updated answer should work.
Evan Teran
A: 

Try:

sudo apt-get install libaio-dev

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

Jared Oberhaus
A: 

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
A: 

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.