Re: Library in library...

From: Greg Comeau (comeau_at_panix.com)
Date: 12/09/03


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?


Relevant Pages

  • Re: Very Slow VSS Development
    ... I concur that it is more than likely JIT but it seems like an awful long ... I'll pull my data tier and compiling it to libs and see if that helps. ... To speed this up, a bit, you can compile the libs and then reference the ... When debugging a ASP.Net web site the compile seem fast but starting the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Very Slow VSS Development
    ... Another factor is how much of the site is in other libs, ... To speed this up, a bit, you can compile the libs and then reference the ... When debugging a ASP.Net web site the compile seem fast but starting the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: z88dk compile problem
    ... to make the libs just change to the libsrc ... I think all download pkgs come with the z80 libraries ... To compile C programs you need to specify a target ... The +zx means Spectrum and the -lndos links to ...
    (comp.sys.sinclair)
  • Re: directx samples
    ... >> headers for compiling, libs for linking, src for the common files. ... > To be exact the d3dapp.cpp wont compile on the examples but everything else ... > the same as the error for the newer PC with larger directx3D programs like ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: Adress of a struct vs address of its first field
    ... >> standard calls PODs) the same rule applies. ... I don't think that the above code is supposed to compile ... >following should compile and yield true for POD types: ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.cpp)