I am having trouble in linking with a static library I created. Here is my directory structure:
test
├── b.c
├── b.o
├── i.h
├── libb.a
└── t
└── a.c
Here is the content of each file:
i.h:
#include <stdio.h>
void f (int);
#define LENGTH 4
b.c:
#include "i.h"
void f (int i)
{
printf ("from b.c: %d\n", i);
}
a.c:
#include "../i.h"
int main (void)
{
f (23);
printf ("%d\n", LENGTH);
}
To build b.o
, I issued: gcc -c i.h b.c
. To build libb.a
, I issued: ar rcs libb.a b.o
.
The command gcc ../b.o a.c
(when issued from inside test/t
, produces a.out
which runs as expected. The problem is when I am trying to link with libb.a
using: gcc -L.. -lb a.c
from inside test/t
. The linker is complaining:
$ gcc -L.. -lb a.c
/usr/bin/ld: /tmp/ccbT50MJ.o: in function `main':
a.c:(.text+0xa): undefined reference to `f'
collect2: error: ld returned 1 exit status
Please let me know what I am missing here.