From: carl on

"Jens Thoms Toerring" <jt(a)toerring.de> wrote in message
news:85j63dFr9oU1(a)mid.uni-berlin.de...
> carl <carl@.com> wrote:
>> In a program I include the file zlib.h. When I include that file I
>> suddenly
>> needs to specify a library file which it should "link against". Why does
>> some .h files need a library file and what does it mean that it links
>> against the library file?
>
> I guess zlib.h isn't a header file you wrote but that comes with
> some packages (probably the zlib package). Then this package
> will include typically (at least) two things, a library and a
> header file.
>
> The library contains the (already compiled) code for all the
> functionality of this package. None of this is in the header
> file. What the header file contains is mostly "declarations"
> of the functions in the library, i.e. their names, their
> return vaue types and the kind of arguments they take.
>
> What are these "declarations" good for? They are mostly meant
> for the compiler. If there's a function you want to use in
> your program and it's declaration in the header file is e.g.
>
> int * inflate( int a, const char * y );
>
> then this will tell the compiler that this function returns
> an int pointer and takes two arguments, an int and a char
> array. Now if you call the function with wrong arguments
> in your program then the compiler can warn you that you do
> something potentially dangerous. And if you assign the return
> value to something else then an int (or void) pointer it also
> will warn you. It's also important for the compiler to know at
> least the return value of the function in order to be able to
> pass it back to your program correctly (in C, if the compiler
> doesn't know the return value type of a function, it will
> blindly assume that it's a simple int - but if this isn't the
> case you could end up with very strange run-time errors).
>
> The necessity to link against the library does not result from
> including the header file - you can include header files all
> day long without needing to link explicitely against a single
> library. The necessity to link against the library comes from
> using a function from the library in your program. These func-
> tions aren't, as pointed out above, in the header files (there
> you find only information what they return and what arguments
> they take), but the code for the functions is only in the li-
> brary. And without linking against the library the linker has
> no idea where to take them from, so it will throw a fit and not
> create your program.
> Regards, Jens

Ok so the declaration is done in the header file and the concrete
implementation is located in the corresponding library file. But is that not
normally what the .cpp file contains?

Typically I first write my declarations in a somefile.h file followed by a
somefile.cpp file which defines (fills out the declarations) the functions.
This somefile.cpp file also includes the somefile.h file. When ever I need
to use/call the functions declared in somefile.h I just include that file.

I don't really see why its necessary to link against somefile.lib since the
concrete implementation is already present in the somefile.cpp file.




From: carl on

"Barry Margolin" <barmar(a)alum.mit.edu> wrote in message
news:barmar-732FA1.21073119052010(a)news.eternal-september.org...
> In article <ht1kuk$n8n$1(a)reader1.panix.com>,
> John Gordon <gordon(a)panix.com> wrote:
>
>> In <4bf447b7$0$281$14726298(a)news.sunsite.dk> "carl" <carl@.com> writes:
>>
>> > In a program I include the file zlib.h. When I include that file I
>> > suddenly
>>
>> "Suddenly" implies that this was not happening before you included the .h
>> file. Is this correct? After adding the statement #include <zlib.h>,
>> your program suddenly stops compiling?
>>
>> > needs to specify a library file which it should "link against". Why
>> > does
>>
>> What is telling you that you "need to specify a library file"? Is it
>> your compiler? Please post the exact error message that you're seeing.
>>
>> > some .h files need a library file and what does it mean that it links
>> > against the library file?
>>
>> It means that while the .h file contains stuff like function prototypes,
>> the actual *code* for those functions is in the library.
>>
>> Think of it like a manual for a washing machine. The manual tells you
>> how to use the machine, what to expect during operation, how to fill it
>> with soap, etc. But if you want to actually wash clothes, the manual
>> alone isn't enough. You still need the machine itself to do the work.
>
> I think what's confusing him is that he doesn't have to specify
> libraries for many other header files that he uses, like <inet.h>,
> <math.h>.

Exactly! And what is it in a header file that triggers the compiler to look
for the corresponding .lib file?

> The reason for this is that there's a library that's normally linked
> with automatically by default (traditionally called something like "the
> standard C runtime library", and usually named libc.a). The functions
> declared in these header files are defined in that library, so it's not
> necessary to link with it explicitly.

ok that makes sense.

>
> But zlib is not a part of the standard runtime, so if you want to use
> its functions you have to say so when compiling.
>
> --
> Barry Margolin, barmar(a)alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

From: John Gordon on
In <4bf5137c$0$280$14726298(a)news.sunsite.dk> "carl" <carl@.com> writes:

> Typically I first write my declarations in a somefile.h file followed by a
> somefile.cpp file which defines (fills out the declarations) the functions.
> This somefile.cpp file also includes the somefile.h file. When ever I need
> to use/call the functions declared in somefile.h I just include that file.

For small programs where you write all the code yourself, that works. But
when you start doing larger projects that use code written by someone else,
it isn't always practical.

--
John Gordon A is for Amy, who fell down the stairs
gordon(a)panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

From: Rainer Weikusat on
"carl" <carl@.com> writes:
> "Jens Thoms Toerring" <jt(a)toerring.de> wrote in message

[...]


>> The necessity to link against the library does not result from
>> including the header file - you can include header files all
>> day long without needing to link explicitely against a single
>> library. The necessity to link against the library comes from
>> using a function from the library in your program.

[...]

> Ok so the declaration is done in the header file and the concrete
> implementation is located in the corresponding library file. But is
> that not normally what the .cpp file contains?
>
> Typically I first write my declarations in a somefile.h file followed
> by a somefile.cpp file which defines (fills out the declarations) the
> functions. This somefile.cpp file also includes the somefile.h
> file. When ever I need to use/call the functions declared in
> somefile.h I just include that file.

Since you are already linking the object code file corresponding with
something.cpp together with your other object code files, it isn't
very surprising that you don't need to do so twice. Things are
different, however ...

> I don't really see why its necessary to link against somefile.lib

.... when you desire to use functions contained in some object code
file (or archive containing multiple object code files) you don't
already link to your program. In this case, you have to add these,
too.
From: David Schwartz on
On May 20, 3:48 am, "carl" <carl@.com> wrote:

> Ok so the declaration is done in the header file and the concrete
> implementation is located in the corresponding library file. But is that not
> normally what the .cpp file contains?

Yes, which is why you need to link to its compiled version.

> Typically I first write my declarations in a somefile.h file followed by a
> somefile.cpp file which defines (fills out the declarations) the functions.
> This somefile.cpp file also includes the somefile.h file. When ever I need
> to use/call the functions declared in somefile.h I just include that file..

Right, and then you link with the compiled version of somefile.cpp.

> I don't really see why its necessary to link against somefile.lib since the
> concrete implementation is already present in the somefile.cpp file.

The somefile.lib file *is* the compiled version of the somefile.cpp
file. The reason you *do* need to link to the library is because it
contains the compiled version of the cpp file that contains the
implementation.

DS