How to use shared library via G++? - c++

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

Related

error while loading shared libraries cannot open shared object file no such file or directory

g++ --shared -fPIC -o foo.so foo.cpp
g++ main.cpp foo.so
./a.out
result:
./a.out: error while loading shared libraries: foo.so: cannot open shared object file: No such file or directory
But:
g++ --shared -fPIC -o foo.so foo.cpp
g++ main.cpp ./foo.so
./a.out
succeed
./foo.so vs foo.so, How so? Thanks!

problem with including fmt library in several cpp files

Good day!
I installed the fmt library in Ubuntu. Added it in my project
#include "fmt/core.h"
#include "fmt/format.h"
#include "fmt/format-inl.h"
to use fmt::format_int и fmt::format. I added library headers in several cpp files of my project. During linkage I got the mistake "multiple definition":
obj/container.o: In function fmt::v7::format_error::~format_error()': container.cpp:(.text+0x40e): multiple definition of fmt::v7::format_error::~format_error()'
obj/line.o:line.cpp:(.text+0x40e): first defined here
I've read something about this mistake. It is recommended to divide declaration and implementation in h and cpp files, to set some status to objects that causes mistake and so on. But all this recommendations imply editing of library (not my!) code!
What is wrong?
I do the following
compilation of files - one by one
g++ -std=c++11 -Wall -o obj/line.o -c /home/...//line.cpp
g++ -std=c++11 -Wall -o obj/container.o -c /home/...//container.cpp
g++ -std=c++11 -Wall -o obj/geometryObject.o -c /...//geometryObject.cpp
g++ -std=c++11 -Wall -o obj/model.o -c /home/...//model.cpp
g++ -std=c++11 -Wall -o obj/point.o -c /home/...//point.cpp
g++ -std=c++11 -Wall -o obj/main.o -c /home/...//main.cpp
Linking - error here
g++ -std=c++11 -Wall -o myapp obj/line.o obj/container.o obj/geometryObject.o obj/model.o obj/point.o obj/main.o
You shouldn't be including fmt/format-inl.h because it's an internal header. Please see the documentation for the list of public headers and what they provide.

position indepdendent code for shared object file under cmake?

We created a shared object file and linked to our executable:
add_executable(a ${A_SRC})
add_library(testso SHARED src/mainlib.cc test1.cc test2.cc)
target_link_libraries(a, testso)
we notice that the code is compiling without -fPIC, but the link uses -fPIC:
g++ -std=c++11 -g -c -i../../src ./test1.cc
g++ -std=c++11 -shared -fPIC ../mainlib.cc test1.o -o testso.so
Is the -fPIC flag required on each file? Will we crash at some point or is this ok?

Compiling Dynamically Linked Library in C++ with Run Path Issue

I'm busy trying to compile and link a c++ program using the following make file
driver.so:driver.cpp
g++ -c driver.cpp -o driver.so
g++ -L/tokenlib/libtokenlib.so driver.so -o linked
but I'm getting the error
g++ -c driver.cpp -o driver.so
g++ -L/tokenlib/libtokenlib.so driver.so -o linked
driver.so: In function `main':
driver.cpp:(.text+0x9): undefined reference to `tokenlib::acquire_token()'
collect2: error: ld returned 1 exit status
make: *** [driver.so] Error 1
and my file hierarchy is
/root
makefile
driver.cpp
/tokenlib
libtokenlib.so
I've been told that adding -Wl,-rpath=./tokenlib to my g++ arguements would solve the problem so I tried like this
driver.so:driver.cpp
g++ -c driver.cpp -o driver.so -Wl,-rpath=./tokenlib
g++ -L/tokenlib/libtokenlib.so driver.so -o linked -Wl,-rpath=./tokenlib
but it still isn't working.
I'm a complete novice and any help would be appreciated.
The linker usually refers to .a (stub) libraries, not to .so files!
-L options specify the paths, the lnker should take in account.
The -l<mystuff> option tries to resolve a library named libmystuff.a from any of the given path's of the -L options, or compiler intrinsic library search paths.

undefined reference to `EVP_CIPHER_CTX_init'

HI,
I have an error: undefined reference to EVP_CIPHER_CTX_init'. I have the encryption code in the header.cpp. In header.h i've initialized the method that i use for encryption. i've created an ar lib.a from header.cpp and header.h. i've added the -lcrypto but still when i run the test.cpp code where i've added in the header the header.h and i compile g++ test.cpp -o test -lib.a i have this error. where am i wrong?
the compilation code:
g++ -c -static -L -lb header.cpp -lcrypto -o lib.o
ar rcs lib.a lib.o
g++ test.cpp -o tst libr.a
in the header.h i did include all the libraries necesarry for the openssl/evp.h and so on. In test.cpp i've included header.h file.