Linking with dynamic library with dependencies

后端 未结 4 1935
情书的邮戳
情书的邮戳 2020-11-30 17:35

Consider the following scenario:

  • Shared Library libA.so ,with no dependencies.
  • Shared Library libB.so, with libA.so as its dependency.

4条回答
  •  死守一世寂寞
    2020-11-30 17:58

    This is an interesting post - I was banging my head with this as well, but I think you miss a point here..

    The idea is as follows, right ?

    main.cpp =(depends)=> libB.so =(depends)=> libA.so
    

    Let's further consider that ..

    • In a.cpp (and only there) you define a class / variable, let's call it "symA"
    • In b.cpp (and only there) you define a class / variable, let's call it "symB".
    • symB uses symA
    • main.cpp uses symB

    Now, libB.so and libA.so have been compiled as you described above. After that, your first option should work, i.e.:

    g++ main.cpp -o main -I. -L. -lB
    

    I guess that your problem originates from the fact that

    in main.cpp you also refer to symA

    Am I correct?

    If you use a symbol in your code, then that symbol must be found in an .so file

    The whole idea of inter-referencing shared libraries (i.e. creating APIs), is that the symbols in the deeper layers are hidden (think of peeling onions) and not used. .. i.e. don't refer to symA in your main.cpp, but only to symB instead (and let symB to refer symA only).

提交回复
热议问题