By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

why link against a library?

P: n/a
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error

thanks

Jun 13 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
happyvalley wrote:
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error


-I and -L specify directories where respectively header files and
libraries can be found. They are different things so they don't
necessarily live in the same place. On a typical Linux system for
instance header files can be found in /usr/include while libraries are
in /lib.
The -l switch tells the linker which specific libraries to link against.
Just like you used #include to tell which header files to use for
compilation of a translation unit.
Header files typically only hold declarations. It just tells what
certain structures look like and which functions exist somewhere so the
compiler knows how to compile a translation unit. After that you still
need the actual code of those functions to create a working program.

See also
http://groups.google.com/group/comp....616babfa4cbc97

--
Dirk

(PGP keyID: 0x448BC5DD - http://www.gnupg.org - http://www.pgp.com)
Jun 13 '06 #2

P: n/a
On 12 Jun 2006 21:57:56 -0700, "happyvalley"
<ni*************@gmail.com> wrote in comp.lang.c++:
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error

thanks


Note that linkers and their operation are not really topical here,
since they are not defined by the language, and differ from one
compilers to another.

But in general, the path or set of paths that you specify to the
linker to search for libraries might contain dozens or hundreds of
them. A linker could be designed to search every single one of them
to see if it contains anything it needs, but there are two problems
with that approach.

The first is that it could make building an executable take much
longer.

The second is that a symbol could be defined in more than one library,
leading either to multiple definitions, or an executable that builds
without error but uses the wrong function or object.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 13 '06 #3

P: n/a
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error


This is merely a platform question than a C++ question.

In typical compile-link tools '-I' is used to specify
an include path, '-L' the path where to find libraries
and '-l' the name of the library. So all three are useful.
HTH

Jun 13 '06 #4

P: n/a
thanks, you mean all libraries should be specified using -lxxx ?
say the hello world program below. there is no problem to compile, link
without specifing any library for printf. why?

#include <iostream>
int main(int argc, char *argv[])
{
printf("hello\n");
return 0;
}

----
thanks
In typical compile-link tools '-I' is used to specify
an include path, '-L' the path where to find libraries
and '-l' the name of the library. So all three are useful.

HTH


Jun 13 '06 #5

P: n/a
* happyvalley:
[top-posting]


Please don't top-post in this group. Read the FAQ. Perhaps also
Wikipedia's article on top-posting (where this group is mentioned).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 13 '06 #6

P: n/a
on the contrary, I think top-posting is a better way than post-posting
or inline posting. sometimes, I receive an email with replies
after/inside my original msg. kind of not convenient to find out the
responses.

the same situation in the group, also, the embedded original msg is
already folded. if I want to know what was going on earlier on this
topic, just roll-down, unfold the eariler posts to check the history. I
did not see anything messed up.
just different ways people used to.

sorry, I really do not see the point of anti-top-posting. actually it
is a convenient way

(OK, one exception, post-posting is better if you submit this group
using email, read msg, reply within your email box.
but it is not a good way to participate such a big group like this. I
assume you don't enjoy your emailbox is stuffed with tons of msg you
might not interested in everyday)

Alf P. Steinbach wrote:
* happyvalley:
[top-posting]


Please don't top-post in this group. Read the FAQ. Perhaps also
Wikipedia's article on top-posting (where this group is mentioned).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Jun 13 '06 #7

P: n/a
On 13 Jun 2006 07:51:10 -0700, "happyvalley"
<ni*************@gmail.com> wrote in comp.lang.c++:
on the contrary, I think top-posting is a better way than post-posting
or inline posting. sometimes, I receive an email with replies
after/inside my original msg. kind of not convenient to find out the
responses.

the same situation in the group, also, the embedded original msg is
already folded. if I want to know what was going on earlier on this
topic, just roll-down, unfold the eariler posts to check the history. I
did not see anything messed up.
just different ways people used to.

sorry, I really do not see the point of anti-top-posting. actually it
is a convenient way

(OK, one exception, post-posting is better if you submit this group
using email, read msg, reply within your email box.
but it is not a good way to participate such a big group like this. I
assume you don't enjoy your emailbox is stuffed with tons of msg you
might not interested in everyday)

Alf P. Steinbach wrote:
* happyvalley:
[top-posting]


Please don't top-post in this group. Read the FAQ. Perhaps also
Wikipedia's article on top-posting (where this group is mentioned).


Ok, *plonk*.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 13 '06 #8

P: n/a
happyvalley wrote:
on the contrary, I think top-posting is a better way than post-posting
or inline posting.


We don't care. Besides the fact that any defense of top-posting is
silly, the prefered method in this group is the one you need to use.
Read the FAQ. If you don't, then people will quickly marginalize you.
Some will killfile you. You won't get useful answers to your questions,
because every thread will turn into one about your posting style.


Brian
Jun 13 '06 #9

P: n/a
thanks for your advice, sounds interesting,

Default User wrote:
happyvalley wrote:
on the contrary, I think top-posting is a better way than post-posting
or inline posting.


We don't care. Besides the fact that any defense of top-posting is
silly, the prefered method in this group is the one you need to use.
Read the FAQ. If you don't, then people will quickly marginalize you.
Some will killfile you. You won't get useful answers to your questions,
because every thread will turn into one about your posting style.


Brian


Jun 13 '06 #10

P: n/a
happyvalley wrote:
thanks for your advice, sounds interesting,


*plonk*

Brian
Jun 13 '06 #11

P: n/a

Default User wrote:
happyvalley wrote:
thanks for your advice, sounds interesting,


*plonk*

Brian

*plonk*

Jun 13 '06 #12

P: n/a
[top-posting adjusted]

happyvalley wrote:
Default User wrote:
happyvalley wrote:
on the contrary, I think top-posting is a better way than post-posting
or inline posting.


We don't care. Besides the fact that any defense of top-posting is
silly, the prefered method in this group is the one you need to use.
Read the FAQ. If you don't, then people will quickly marginalize you.
Some will killfile you. You won't get useful answers to your questions,
because every thread will turn into one about your posting style.


thanks for your advice, sounds interesting,


I'd suggest you take Ambrose's advice to Augustine to heart: "Si fueris
Romae, Romano vivito more; si fueris alibi, vivito sicut ibi." (If you
are in Rome, live in the Roman way; if you are elsewhere, live as they
do there.)

Or else.

*plonk*

Cheers! --M

Jun 13 '06 #13

P: n/a

happyvalley wrote:
Default User wrote:
happyvalley wrote:
thanks for your advice, sounds interesting,


*plonk*

Brian

*plonk*


I see advice is not only wasted on you but you respond with rudness as
thanks. I shall keep this in mind for your future queries.

Jun 13 '06 #14

P: n/a
Noah Roberts wrote:
happyvalley wrote:
Default User wrote:
happyvalley wrote:

> thanks for your advice, sounds interesting,

*plonk*

Brian

*plonk*


I see advice is not only wasted on you but you respond with rudness as
thanks. I shall keep this in mind for your future queries.


he plonked me, certainly I plonked him.

I don't think, another top-posting can be considered as "rude",
comparing to his explicit statement "any defense of top-posting is
silly," as his reply,

if you say rude, he was rude first, read his voice. this is not the
way people should talk.

Jun 13 '06 #15

P: n/a
Noah Roberts wrote:

happyvalley wrote:
Default User wrote:
happyvalley wrote:

> thanks for your advice, sounds interesting,

plonk

Brian

plonk


I see advice is not only wasted on you but you respond with rudness as
thanks. I shall keep this in mind for your future queries.


Piggybacking on your message for obvious reasons. I'm not at all sure
that "happyvalley" knows what plonk means. He's posting via Google
Groups, and unless things changed recently, there's no killfile
ability. How he plonked me is one of those great mysteries.


Brian
Jun 13 '06 #16

P: n/a

Default User wrote:
Noah Roberts wrote:

happyvalley wrote:
Default User wrote:
> happyvalley wrote:
>
> > thanks for your advice, sounds interesting,
>
> plonk
>
>
>
> Brian
plonk


I see advice is not only wasted on you but you respond with rudness as
thanks. I shall keep this in mind for your future queries.


Piggybacking on your message for obvious reasons. I'm not at all sure
that "happyvalley" knows what plonk means. He's posting via Google
Groups, and unless things changed recently, there's no killfile
ability. How he plonked me is one of those great mysteries.


Brian


true, I am posting on google without that funcation. I just want to
show my position. I don't want to hear somebody who don't want to hear
my voice.

This is a place for helps and offering helps if you like. I believe all
value and appreciate others's kind help.

true, it is better to follow some rule to make things easier. however,
there is no reason to be so sensitive. Somebody "violate" some
religion-like "rule". he/she will be marginalized/ kicked out of the
circle.
yes, It is your freedom to block anybody's posts, like you want to
answer a question or not. If you want, just killfile anybody on your
local machine silently. While there is no reason to show your "anger"
for voliating your religion to the whole group. this can be considered
as a "rude" behavior in some sense.

don't like this kind of air if this is what this group really like. It
is funny to be rude and narrow-minded.

Jun 13 '06 #17

P: n/a
happyvalley wrote:
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error


Gee, I already have /usr/bin in my PATH! Why do I have to type specific
commands?

:)

Jun 13 '06 #18

P: n/a

Kaz Kylheku wrote:
happyvalley wrote:
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error


Gee, I already have /usr/bin in my PATH! Why do I have to type specific
commands?

:)


haha,
The reason I got this question was you don't need to specify library
some time (say hello world program). this is because the compiler sets
some libraries as default, need not to specify. right.

anyway, thanks

Jun 13 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.