Ebrahim Ghasemi
Ebrahim Ghasemi

Reputation: 6126

How to use shared library via G++?

I have a library that named matrix and used in my program that named test.cpp.

I can generate and use static library successfully, but when I want to use it as a shared library, I receive the following error :

ap1019@sharifvm:~/the03-copy$ ls
matrix.cpp  matrix.h  test.cpp
ap1019@sharifvm:~/the03-copy$ g++ -c matrix.cpp
ap1019@sharifvm:~/the03-copy$ g++ -shared -Wl,-soname,matrix.so -o matrix.so matrix.o
ap1019@sharifvm:~/the03-copy$ ls
matrix.cpp  matrix.h  matrix.o  matrix.so  test.cpp
ap1019@sharifvm:~/the03-copy$ g++ test.cpp matrix.so
ap1019@sharifvm:~/the03-copy$ ./a.out
./a.out: error while loading shared libraries: matrix.so: cannot open shared object file: No such file or directory
ap1019@sharifvm:~/the03-copy$

Does anyone have any idea?

Upvotes: 0

Views: 1292

Answers (1)

adnan kamili
adnan kamili

Reputation: 9465

It is better to follow the naming convention for shared libraries.You are linking in a wrong manner.

Check out following for details:

g++ -L/home/username/matrix -Wall -o test test.cpp -lmatrix

http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

Upvotes: 1

Related Questions