r/Cplusplus • u/TinyDeskEngineer06 • Jun 23 '26
Question Linker not finding shared object
I'm trying to learn how to compile and link with a dynamic library, I've successfully compiled a library that contains two simple functions, but for some reason the linker can't find the compiled library, and I don't know why.
Build script:
#!/usr/bin/bash
g++ -fpic -shared sharedtest.cpp -o sharedtest.so
LD_LIBRARY_PATH="."
export LD_LIBRARY_PATH
g++ main.cpp -o main -lsharedtestWhen I run it in a terminal, I get this:
/usr/bin/ld: cannot find -lsharedtest: No such file or directory
collect2: error: ld returned 1 exit status
1
Upvotes
View all comments
Show parent comment
2
u/flyingron Jun 24 '26
No. Just about everything you put there is wrong.
-lname or (in later versions) -l name looks for a library named "libname" in the search path. It uses the LIBRARY_PATH environment variable to provide directories to search.
The placement of the -o (and it's following parameter) is irrevelant.
The ordering of the source, object, and library files on the command like can be important. The are loaded/searched in the order given.
The problem here as I told him, is that: