I am trying to learn the shared library concepts on linux using GCC. So I have created a simple library.
library.c
int foo(void) {
return 10;
}
This is compiled using,
cc -fPIC -g -c library.c
cc -shared -fPIC -Wl,-soname,libmytest.so.1 -o libmytest.so.1.0.1 library.o -lc
It created the file libmytest.so.1.0.1 in the current directory. Now I am writing a client to consume this library in the same directory.
client.c
#include <stdio.h>
extern int foo(void);
int main()
{
int a = foo();
printf("a is %d", a);
return 0;
}
compiling using,
cc client.c -o client -lmytest
but this exits with the message
/usr/bin/ld: cannot find -lmytest
collect2: ld returned 1 exit status
Can anyone help me to find out what I am doing wrong here?
Try using a
-Loption which is used to add a directory to the list of directories that are searched for the-loption:Assuming the
.sois present in the same directory asclient.c. If not add suitable path.The linker on seeing
-lmytestlooks forlibmytest.sobut you have a version number appended to it so it does not work. Way to fix this is to create a symlink namedlibmytest.sopointing tolibmytest.so.1.0.1Alternatively you can use the complete library name on the compile/link line as: