iampratham
iampratham

Reputation: 1

Unable to link ".so" shared library to main ".c" file while compiling

I am trying to make a shared library for a particular problem I was working on. It has "point_sense.c" as the main file which uses functions defined in "createPolygon.c." The functions are declared in a header file "createPolygon.h."

To compile them, I used a makefile which looks like the following


all:point_sense

createPolygon.o:createPolygon.c
    g++ -c -fpic createPolygon.c

libcreatePolygon.so:createPolygon.o
    g++ -shared -o libcreatePolygon.so createPolygon.o

point_sense:point_sense.c libcreatePolygon.so
    g++ -o point_sense -L~Desktop/Summer_2020_linux/tutorials/cpp_practise point_sense.c -lcreatePolygon

clean:
    rm point_sense createPolygon.o libcreatePolygon.so

but when I make the file, it gives an output as

g++ -c -fpic createPolygon.c
g++ -shared -o libcreatePolygon.so createPolygon.o
g++ -o point_sense -L~Desktop/Summer_2020_linux/tutorials/cpp_practise point_sense.c -lcreatePolygon
/usr/bin/ld: cannot find -lcreatePolygon
collect2: error: ld returned 1 exit status
make: *** [makefile:10: point_sense] Error 1

Initially I thought this was some silly mistake, and to check I used

ld -L~/Desktop/Summer_2020_linux/tutorials/cpp_practise -lcreatePolygon -verbose

and after a long output I got (a few unimportant lines in the code are skipped in between)

ld: mode elf_x86_64
attempt to open ~/Desktop/Summer_2020_linux/tutorials/cpp_practise/libcreatePolygon.so failed
attempt to open ~/Desktop/Summer_2020_linux/tutorials/cpp_practise/libcreatePolygon.a failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libcreatePolygon.so failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libcreatePolygon.a failed
.
.
.
ld: cannot find -lcreatePolygon

But when I try to open 'libcreatePolygon.so' directly, I am able to open it.

$ nano ~/Desktop/Summer_2020_linux/tutorials/cpp_practise/libcreatePolygon.so

There are several threads which explain the process of doing this, but I don't see what it is that I am doing wrong. Any help is appreciated. I am using Ubuntu 20.04.1 LTS and g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0 .

Upvotes: 0

Views: 463

Answers (1)

coelhudo
coelhudo

Reputation: 5080

I tried to reproduce the problem here, and this error message goes away if you put a space between the -L flag and the tilde character.

The reason is: if there is no space between -L and ~, the tilde character cannot be expanded to the home directory.

Upvotes: 3

Related Questions