3

In eclipse I have done the below in windows platform with MinGW compiler - C_proj.exe--->links---->libB_proj.a----->links---->libA_proj.a

  1. When linking A with B I am specifying only the path to libA_proj.a (not the lib name because that is not possible it seems!)

  2. When making use of B inside C I had to specify both the libA_proj.a and libB_proj.a.

Codes -

//a.h
    class a
    {
    public:
        void printA();
    };

//a.cpp    
    void a::printA()
    {
        std::cout<<"This is 'a'"<<std::endl;
    }
//b.h
    class b
    {
    public:
        void printB();
        void printA();
    };
//b.cpp
    void b::printB()
    {
        std::cout<<"This is 'B'"<<std::endl;
    }

    void b::printA()
    {
        std::cout<<"This is 'B'....calling A"<<std::endl;
        a objA;
        objA.printA();
    }

//c.cpp    
    int main()
    {
        b objB;
        objB.printB();
        objB.printA();
        return 0;
    }

Now my requirement is -- I don't want to link both B and A with C exe. Is there a way to link only B with C and get the work done so that I can provide only B and C exe to a third party?

Soumyajit Roy
  • 463
  • 2
  • 8
  • 17
  • There is definitely a way, but I don't know how to do uit with your tools. [This is how you would do it in Visual Studio](http://i.imgur.com/X2hE2Bu.jpg) – typ1232 Jul 29 '13 at 12:38
  • @typ1232: I think that assumes the `.lib` is a thunk-library for a .dll – Mats Petersson Jul 29 '13 at 12:40
  • http://stackoverflow.com/questions/12198261/include-static-library-in-static-library – Derek Jul 29 '13 at 13:13
  • @typ1232 - Thanks man!! Superb....that works like butter!!! :) Actually I work in VStudio only....trying that in light weight eclipse!! :D – Soumyajit Roy Jul 29 '13 at 13:18

1 Answers1

2

A lib-file (or .a file) is a collection of object files - a bit like a zip-file, but with no compression. So you can't really LINK an lib file with another lib file.

What you can do is form one large library from two smaller ones - essentially unpacking and then repacking as one. You use ar -x libfile.a to extract modules, and then you can use ar -a libfile2.a name1.o name2.o to add the object files into the new library.

However, I would just tell the users of your two components to link against both libraries. Much easier solution.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • [1] g++ -c a.cpp -o a.o [2] ar rcs liba.a a.o [3] g++ -c b.cpp -o b.o -L. -la [4] ar rcs libb.a b.o [5] g++ c.cpp -L. -lb -la ...........Can u tell me what to do instead of this??? :( – Soumyajit Roy Jul 29 '13 at 12:49
  • Yes, `ar rcs liba.a a.o b.o` – Mats Petersson Jul 29 '13 at 12:51
  • In other words, you build one library instead of two. There is no way you can make two libraries into one by "linking them". You have to build one library that contains all the necessary object files. If that, for some logistical reasons isn't a good solution, then you need to link to both libraries in the final product. – Mats Petersson Jul 29 '13 at 12:52
  • I understood your point...Tried your solution. That works! Thanks... :) – Soumyajit Roy Jul 29 '13 at 13:02