简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM