(I'd rather have foo. Sh than lib/foo. Sh but I'll fix that later) There's most of your problem: the in the name stops the dynamic linker from doing the rpath magic (Your rpath is wrong too.
Think about it: from the shell, if you were currently in the directory where your executable is, how would you get to the directory where your library is? Here, you'd need to cd ../lib So your rpath should be $ORIGIN/../lib ) If you built your object as libfoo. So and linked with Llib -lfoo the linker would work out what you were intending, and do the right thing.
But if you're going to use unusual naming conventions, you'll have to help it out: Change the link line for the library to explicitly set the SONAME for your library to just foo. Sh : g++ -shared -Wl,-soname,foo. Sh -o lib/foo.Sh obj/foo.
O Fix the rpath: g++ -o run/main. Run obj/main. O -Wl,-rpath,'$ORIGIN/../lib' -Llib -l:foo.
Sh It's useful to run ldd main/main. Run to see what's going on.In your original failing case, you'll see something like: lib/foo. Sh (0xNNNNNNNN) (the lack of any some/resolved/path showing that it's not done any path resolution).
In the fixed case, you'll see something like: foo.Sh => /your/path/to/run/../lib/foo. Sh (0xNNNNNNNN).
(I'd rather have foo. Sh than lib/foo. Sh but I'll fix that later).
There's most of your problem: the / in the name stops the dynamic linker from doing the rpath magic. (Your rpath is wrong too. Think about it: from the shell, if you were currently in the directory where your executable is, how would you get to the directory where your library is?
Here, you'd need to cd ../lib. So your rpath should be $ORIGIN/../lib. ) If you built your object as libfoo.So and linked with -Llib -lfoo, the linker would work out what you were intending, and do the right thing.
But if you're going to use unusual naming conventions, you'll have to help it out: Change the link line for the library to explicitly set the SONAME for your library to just foo. Sh: g++ -shared -Wl,-soname,foo.Sh -o lib/foo. Sh obj/foo.
O Fix the rpath: g++ -o run/main. Run obj/main. O -Wl,-rpath,'$ORIGIN/../lib' -Llib -l:foo.
Sh It's useful to run ldd main/main. Run to see what's going on. In your original failing case, you'll see something like: lib/foo.
Sh (0xNNNNNNNN) (the lack of any => /some/resolved/path showing that it's not done any path resolution). In the fixed case, you'll see something like: foo. Sh => /your/path/to/run/../lib/foo.
Sh (0xNNNNNNNN).
Thanks, finally worked. (I had tried #2, and #1 but with libfoo. So, and not at same time :().
- As for the ../../, I was unsure whether $ORIGIN was the executable or the folder it resided in. After testing a few more things I'll fix this post up as a working example. – Simon Jun 11 at 13:47 You mention the / messes up the dynamic linker.
I hope I'm going to be able to put foo. So in project/lib/dir and use dir/foo.so. Going to try now.
– Simon Jun 11 at 13:49.
This is a example of relative-path linking (with ld) by using $ORIGIN in an rpath. Rpath is a path (or set of paths) embedded in binary files (shared libraries (.so) and executables). These paths are the foremost search paths for the shared libraries the binary must be linked with at runtime.
$ORIGIN is a potential start directory for an rpath path. It resolves to the directory containing the executing file. (eg: $ORIGIN/lib) The example project builds one shared library and one executable that links to said library using rpath and $ORIGIN.
You can download the project from here. File structure (before building): project/ src/ foo. Cpp main.
Cpp make. Sh project/src/foo. Cpp int foo() { return 3; } project/src/main.
Cpp int foo(); #include int main() { std::cout embeds into the generated library. This should match the name you supply for the -l or -l: options when linking to this library. -Wl,-rpath,'' embeds into the generated library as a runtime library search path (or rpath - see above).
-L adds a path to the build-time library search path list. (Note: rpath is irrelevant at build-time, -L is irrelevant at runtime). -l: adds the filename (without path) of a library to link against.(Similar to -l, except -l: requires the complete filename.
File structure (after building): project/ src/ foo. Cpp main. Cpp obj/ foo.
O main. O lib/ dir/ foo. So run/ main.
Run make. Sh Note: I'm using -l: which requires the complete library filename (amongst other reasons, it's easier to script with variables when all functions take the same name format). It is more common to use -l, whereby -l denotes lib.so.
Limitations As far as I'm aware (correct me if I'm wrong) there's no way to add a library inside a subdirectory within a search path (except for adding that directory as a sub-path). This is true for both build-time (-L) and run-time (-rpath) search paths. So if you have two libraries with the same name but different locations, you won't be able to link them both.(I hope I'm wrong or that this gets fixed).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.