public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* I can't compile this program!
@ 2003-02-14  5:25 japan
  2003-02-15 17:04 ` Jakub Ilski
  0 siblings, 1 reply; 6+ messages in thread
From: japan @ 2003-02-14  5:25 UTC (permalink / raw)
  To: gcc-help

I want to ask some questions!

I have an ANSI C program named test.c:

/*test.c*/
#include <stdio.h>
#include <math.h>
main()
{
  printf("%f\n",sqrt(4));
  return 0;
}

I can't compile this code. I used command:

gcc -o test test.c

Please tell me why and how to compile that code!

Thanks.


__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I can't compile this program!
  2003-02-14  5:25 I can't compile this program! japan
@ 2003-02-15 17:04 ` Jakub Ilski
  2003-02-17 15:58   ` japan
  0 siblings, 1 reply; 6+ messages in thread
From: Jakub Ilski @ 2003-02-15 17:04 UTC (permalink / raw)
  To: japan, gcc-help

Hi.
On Friday 14 February 2003 06:25, japan wrote:
> I want to ask some questions!
>
> I have an ANSI C program named test.c:
>
> /*test.c*/
> #include <stdio.h>
> #include <math.h>
> main()
> {
>   printf("%f\n",sqrt(4));
>   return 0;
> }
>
> I can't compile this code. I used command:
>
> gcc -o test test.c
>
> Please tell me why and how to compile that code!
>
> Thanks.

You have to tell linker to add 'libm[ath]' library.

try:

gcc -lm -o test test.c



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I can't compile this program!
  2003-02-15 17:04 ` Jakub Ilski
@ 2003-02-17 15:58   ` japan
  2003-02-17 16:02     ` Mihnea Balta
  0 siblings, 1 reply; 6+ messages in thread
From: japan @ 2003-02-17 15:58 UTC (permalink / raw)
  To: gcc-help

Although I included math.h in the source code, why I must add -lm
option when we compile one program using math functions???

Ex: gcc -lm -o test test.c

(test.c uses math functions and have already included math.h!)

What libs we must link when compiling like that?

Thanks very much!

__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I can't compile this program!
  2003-02-17 15:58   ` japan
@ 2003-02-17 16:02     ` Mihnea Balta
  0 siblings, 0 replies; 6+ messages in thread
From: Mihnea Balta @ 2003-02-17 16:02 UTC (permalink / raw)
  To: japan, gcc-help

On Monday 17 February 2003 17:58, japan wrote:
> Although I included math.h in the source code, why I must add -lm
> option when we compile one program using math functions???
>
> Ex: gcc -lm -o test test.c
>
> (test.c uses math functions and have already included math.h!)
>
> What libs we must link when compiling like that?

You included the header file, which contains the function prototypes, 
#defines, etc. That's got nothing to do with the linker, which must be told 
to link your code against the math library (libmath). The -lm flag tells it 
to do that. Go learn the difference between a header and a shared library.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I can't compile this program!
@ 2003-02-17 19:59 Dockeen
  0 siblings, 0 replies; 6+ messages in thread
From: Dockeen @ 2003-02-17 19:59 UTC (permalink / raw)
  To: gcc-help

> Although I included math.h in the source code, why I must add -lm
> option when we compile one program using math functions???

You must remember what a header is.  It is a data file that contains
definitions of what functions take and give back.  There is no code
in a header file.  The guts of the math functions are contained in
the math library, and the command -lm is what links (makes available)
that code.

Note also that it is not just enough to have the library in the 
library link search path, you have to link it through the link command.

Also, one other thing that fouls up folks is that, if you have a 
library names something like xyz, the library file will probably have a
name something like libxyz.a, but when you do the link, you use the 
library name, not the library file name.

Wayne Keen

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: I can't compile this program!
@ 2003-02-14  5:30 Ajay Bansal
  0 siblings, 0 replies; 6+ messages in thread
From: Ajay Bansal @ 2003-02-14  5:30 UTC (permalink / raw)
  To: japan, gcc-help

Link the program with math library

gcc -o test test.c -lm

I feel that every systemshould follow the man page standards of solaris
(if it is not proprietery). In their man page they mention the library
to be linked with.. Like


Mathematical Library                     sqrt(3M)



NAME
     sqrt - square root function

SYNOPSIS
     cc [ flag ... ] file ... -lm [ library ... ]
     #include <math.h>

     double sqrt(double x);

DESCRIPTION
     The sqrt() function computes the square root of x.


-----Original Message-----
From: japan [mailto:ngmnhat@yahoo.com] 
Sent: Friday, February 14, 2003 10:56 AM
To: gcc-help@gcc.gnu.org
Subject: I can't compile this program!


I want to ask some questions!

I have an ANSI C program named test.c:

/*test.c*/
#include <stdio.h>
#include <math.h>
main()
{
  printf("%f\n",sqrt(4));
  return 0;
}

I can't compile this code. I used command:

gcc -o test test.c

Please tell me why and how to compile that code!

Thanks.


__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-02-17 19:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-14  5:25 I can't compile this program! japan
2003-02-15 17:04 ` Jakub Ilski
2003-02-17 15:58   ` japan
2003-02-17 16:02     ` Mihnea Balta
2003-02-14  5:30 Ajay Bansal
2003-02-17 19:59 Dockeen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).