I want to create shared object B that dynamically links to shared object A. I'm using the following command to compile shared object B:

```
g++ -fPIC -shared -L/path/to/directory -lA -o libB.so B.cpp

```

It is my understanding that `-lA` is what tells the linker that `libB.so` should dynamically link to `/path/to/directory/libA.so`. However, when I do `ldd` on the final product, the dependency is not listed (and loading `libB.so` fails because of these missing dependencies).

```
ldd libB.so

linux-vdso.so.1 =>  (0x00007ffd4233e000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f35072fe000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f35070e7000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f3506de1000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3506a1c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3507807000)

```

Am I wrong about what `-l` is supposed to do? I assume that the above is a minimal set of dynamic dependencies from C++.

Are there any gotchas to look for? For instance, does the linker simply ignore `-l` requests when it can't find the file or something (and I have to debug my paths more than I already have)?

Do I have to put something in my C++ code to indicate dependency (like "extern" functions or something)?

**Update:**

I have determined that the set of dynamic dependencies that `ld` reports *does* depend on my C++ code and *does not appear to* depend on any `-L` or `-l` flags I supply. The linker is automatically guessing which shared objects my `libB.so` should depend on, and it's not assuming enough.

For instance, I know that I'll need B to load A because I call some code that eventually calls code in `libA.so`. How do I provide this information to the linker?

**Clarifications:**

What I'm calling "shared object A" is a complex thing that may load some code dynamically. I want to include enough dependencies so that it will not fail with "missing symbols" when it tries to load this code dynamically. That's why I want to force dependencies in B, because the linker might not statically find them in the dependency tree.

Also, I'm using g++ 4.8.4 (Ubuntu 14.04). This is relevant because g++ started implicitly applying `-Wl,--as-needed` as of version 4.6.