mcede mcede - 1 year ago 76
C Question

Problems linking during compile of apue related code

I am trying to compile code related to the book "Advanced Programming in the UNIX® Environment"

When I try to compile a test file like so:

$ gcc -L ../lib/ -l apue foo.c

I get:

/tmp/cccXkUae.o: In function `main':
foo.c:(.text+0x2b): undefined reference to `err_sys'
collect2: error: ld returned 1 exit status

However, it seems the function is defined in the lib...

$ grep err_sys ../lib/libapue.a
Binary file ../lib/libapue.a matches

Ultimately, this does compile with no errors:

$ gcc foo.c ../lib/error.c

Just trying to understand what I am doing wrong.

Answer Source

The source code available at gets you (after building) a header named apue.h in include/ and a static lib named libapue.a in lib. So to compile and link against libapue, you need:

gcc -I $apue_root/include -L $apue_root/lib your_file.c -lapue
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download