Re: Library in library...
From: Greg Comeau (comeau_at_panix.com)
Date: 12/09/03
- Next message: Chris \( Val \): "Re: fstream"
- Previous message: Greg Comeau: "Re: fstream"
- In reply to: James Connell: "Re: Library in library..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 9 Dec 2003 09:13:59 -0500
In article <vtb4uv4gkmif37@corp.supernews.com>,
James Connell <jconnell@gci.net> wrote:
>Sweep wrote:
>> A question about linking with a static library that uses code from another
>> static library.
>>
>> Suppose we have files A.h and A.cpp defining class A:
>>
>> //////////////////////
>> // A.h
>> class A {
>> public:
>> A();
>> int get_value();
>> };
>> //////////////////////
>>
>> //////////////////////
>> // A.cpp
>> #include "A.h"
>> A::A() {}
>> int A::get_value() {
>> return 1;
>> }
>> //////////////////////
>>
>> We compile A.cpp and create a library A.lib.
>>
>> Now suppose we have another set of files B.h and B.cpp defining class B:
>>
>> //////////////////////
>> // B.h
>> #include "A.h"
>> class B {
>> public:
>> B();
>> int get_value();
>> private:
>> A a();
>> };
>> //////////////////////
>>
>> //////////////////////
>> // B.cpp
>> #include "B.h"
>> B::B() {}
>> int B::get_value() {
>> return (2*a.get_value());
>> }
>> //////////////////////
>>
>> Now suppose that we have a file test.cpp using class B:
>>
>> //////////////////////
>> // test.cpp
>> #include <iostream.h>
>> #include "B.h"
>> B b();
>> cout << b.get_value();
>> //////////////////////
>>
>> In order for this file to be built the following must be true (correct me if
>> I am wrong):
>> 1. the compiler must be aware of the paths to both A.h and B.h
Yes, but one possibility it to put them both in the same place.
Another might be to literally put a.h into b.h and be done with a.h,
but in many cases that is unnecessary and many even be problematic.
>> 2. both A.lib and B.lib must be given to the linker
That's possible too. Most dev tools let you merge the too though,
so check out the documentation. Note for some, you may need to
"un'lib" one or both of the libs into .obj (or whatever they are
called for your system) and then "re-lib" it. Of course, this
assumes the two libs are mutually exclusive and don't contains
multiple definitions of a global function or something like that
(actually, this too depends upon the dev tools).
>> So, in order for one to use B.lib, one should also have, or be willing to
>> provide paths and other arguments about, A.lib!
>>
>> So the question is:
>>
>> Is there a way to create B.lib and an accompanying header file (say B2.h) in
>> such a way so that I only need to include this B2.h and link to B.lib in
>> order to compile a module that uses class B and create an executable? In
>> other words, is it possible to build B2.h and B.lib in such a way that it
>> B.lib is redistributable without needing to be accompanied by A.h and A.lib?
>
>i would point out that once B.cpp is compiled and added to a lib you
>don't need A.h to use it. i see no way of getting around distributing
>a.lib though. that doesn't mean you need to distribute 2 seperate libs
>however just make one lib of the two and don't let the client *see* the
>calls to A
Right, one doesn't need a.h to use b.obj itself, but other uses
of class B might require a.h, or at least class A to be presented
in some manner, so in the end a.h might be distributed as well.
-- Greg Comeau/4.3.3:Full C++03 core language + more Windows backends Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
- Next message: Chris \( Val \): "Re: fstream"
- Previous message: Greg Comeau: "Re: fstream"
- In reply to: James Connell: "Re: Library in library..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|