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?

4
  • 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 Commented Jul 29, 2013 at 12:38
  • @typ1232: I think that assumes the .lib is a thunk-library for a .dll Commented Jul 29, 2013 at 12:40
  • stackoverflow.com/questions/12198261/… Commented Jul 29, 2013 at 13:13
  • @typ1232 - Thanks man!! Superb....that works like butter!!! :) Actually I work in VStudio only....trying that in light weight eclipse!! :D Commented Jul 29, 2013 at 13:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

[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??? :(
Yes, ar rcs liba.a a.o b.o
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.
I understood your point...Tried your solution. That works! Thanks... :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.