MNayer
MNayer

Reputation: 181

Shared library not found in /usr/local/lib

Similar questions got asked a lot, but I still don't quite get what's wrong with how I compiled and installed my shared library.

As far as compiling goes I do

> gcc -c -fPIC libt.c
> gcc -shared -Wl,-soname,libt.so.0 -o libt.so.0.1 libt.o

In order to install the library I run

> cp libt.so.0.1 /usr/local/lib/
> cp libt.h /usr/local/include/
> ln -s /usr/local/lib/libt.so.0.1 /usr/local/lib/libt.so.0 # ldconfig would setup this symlink itself ...
> ln -s /usr/local/lib/libt.so.0 /usr/local/lib/libt.so # ... but not this one, so I do it myself
> sudo ldconfig

/usr/local/lib is included in /etc/ld.so.conf.d/libc.conf, and ldconfig -p | grep libt yields

libt.so.0 (libc6,x86-64) => /usr/local/lib/libt.so.0
libt.so (libc6,x86-64) => /usr/local/lib/libt.so

So, as far as I can tell, everything looks okay until this point. However, compiling a program that's supposed to use my library fails:

> gcc -o prog main.c -llibt
/usr/bin/ld: cannot find -llibt

libt.h

#ifndef libt_h__
#define libt_h__

extern int add(int, int);

#endif 

libt.c

int
add(int a, int b)
{
    return a + b;
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "libt.h"

void
print_usage()
{
    printf("usage: ./prog <number a> <number b>\n");
}

int
main(int argc, char *argv[])
{
    int a = 0, b = 0, c = 0;

    if (argc != 3) {
        print_usage();
        return 1;
    }

    a = atoi(argv[1]);
    b = atoi(argv[2]);
    c = add(a, b);

    printf("%d\n", c);

    return 0;
}

Upvotes: 0

Views: 864

Answers (1)

MNayer
MNayer

Reputation: 181

Figured it out. While library names have to be prefixed with "lib", that prefix must not be specified when linking. That is, gcc -o prog main.c -llibt is wrong while gcc -o prog main.c -lt works as expected.

Upvotes: 1

Related Questions