Jump to content

about sqrt

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Guest_fxg03_*

Guest_fxg03_*
  • Guests
Hi,
I am a beginner for C. I tried to make a simple program to use sqrt function to calculate square root. But I got some trouble.

Here is the simple program calculate.c:


#include <math.h>

#include <stdio.h>

#include <stdlib.h>

 main()

{ int i= 10;

   double j;

   j= sqrt (i);

}



Then I did the following:
cc calculate.c
And I got the error message:

/tmp/cc5EiCqg.o(.text+0x2a8d): In function 'main':
:undefined reference to 'sqrt'
collect2: ld returned 1 exit status

The system I used is Linux, and I don't know why this happens.
anybody can help me?

#2
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
I can't see any of your includes because you didn't put it in a code segment but it as long as you have math.h included it should work. You just need to link to the math library

"cc calculate.c -lm"

Just tried it and it works perfectly.

#3
Guest_fxg03_*

Guest_fxg03_*
  • Guests
Thanks Lop.
It works now.

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Added code tags around the block of code. The < and > arn't working for some reason.

<string.h>

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
i thought if you include header files or something you go like

#include "stdafx.h"

but if u include a library you use the < and >

#include <iostream>


#6
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Good question sidewinder...... I'm pretty sure either way will work for either. That is odd though, I've never really noticed that until you pointed it out. Maybe someone else can shed some light on this.

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
The file name that resides in between < and > refers to a system file, double quotes can be used to specify a user-defined (non-system) file.