user1918858
user1918858

Reputation: 1208

C++ Linking one library through the other

libA.so links to libB.so

main links to libB.so but it can't access symbols of libA.so. Why? Is there a way i can make symbols of libA.so visible to main through libB.so

cat a.cc 
#include<iostream>
void hello() {
   return;
}

cat  b.cc
#include <iostream>
void goodbye() {
  return;
}


cat c.cc m
#include<iostream>
void goodbye();
void hello();
void tc() {
  goodbye();
  hello();
}


cat main.cc
#include<iostream>
void hello();
void goodbye();
int main() {
   hello();
}

Not sure why this isn't working, how can i make it work?

g++ -fPIC -c a.cc b.cc 
g++ a.o b.o -shared -o libA.so


g++ -fPIC -c c.cc
g++ c.o -shared -lA -L. -o libB.so 

g++ main.cc -lA -L.
g++ main.cc -lB -L.
/tmp/cci2foLo.o: undefined reference to symbol '_Z5hellov'
./libA.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

ldd libB.so 
linux-vdso.so.1 =>  (0x00007fffa733d000)
libA.so => ./libA.so (0x00002b0f9a92a000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00002b0f9ab5e000)
libm.so.6 => /lib64/libm.so.6 (0x00002b0f9ae64000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00002b0f9b0e8000)
libc.so.6 => /lib64/libc.so.6 (0x00002b0f9b2ff000)
/lib64/ld-linux-x86-64.so.2 (0x00002b0f9a504000)

How can i link main to A through B?

Upvotes: 1

Views: 46

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

Dynamic libraries are very similar to normal executable programs, and are separate and distinct entities that are loaded separately.

And they only export their own symbols, not the ones of libraries it links to.

That means main.cc can use the functions, classes, and symbols in general from the libraries that it explicitly link to. If you want to use symbols from the library A you need to link with the library A.

Upvotes: 1

Related Questions