LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   ANSI C: Math library making #include unnecessary? (http://www.linuxquestions.org/questions/programming-9/ansi-c-math-library-making-include-unnecessary-378867/)

olspookishmagus 11-01-2005 04:13 AM

ANSI C: Math library making #include unnecessary?
 
Hello and special greetings to all linuxquestions.org regulars.
This is my first post here, so please be gentle.

I have subscribed to a computer course which has object to teach us ANSI C.

In one of the example the is supposed to be a program that displays some mathematic table containing both angles and relevant sins.

The source file angles-sins.c has the following line:
Code:

#include <math.h>
When I tried to compiled it I typed:
Code:

debian-sarge:~ # gcc -x c angles-sins.c
Then I was fed with an error that said that there were no reference on the function sin which is in the source file.

I edited the source file and made that include line to look like this:
Code:

#include </usr/share/include/math.h>
In case the linker had problem finding the file math.h but still it didn't work.

Reading more carefully my notes I saw that in order to have this compiled you have to to type:
Code:

debian-sarge:~ # gcc -x c angles-sins.c -lm
Which instructs the linker to include the math function library.
The compilation runs error-free this way.

Being more curious I removed the:
Code:

#include <math.h>
line completely and when I typed:
Code:

debian-sarge:~ # gcc -x c angles-sins.c -lm
it compiled again.

My question is:
Why do I have to type the
Code:

#include <math.h>
line in my source code when only by using:
Code:

debian-sarge:~ # gcc -x c foo.c -lm
gets my compilation done?

TruongAn 11-01-2005 05:27 AM

Hi.
I don't have much experience about ANSI C and gcc.
But I have one suggest for you:
Why don't you try some IDE like KDevelop?

Wim Sturkenboom 11-01-2005 05:37 AM

Re: ANSI C: Math library making #include unnecessary?
 
Quote:

Originally posted by olspookishmagus
I edited the source file and made that include line to look like this:
Code:

#include </usr/share/include/math.h>
In case the linker had problem finding the file math.h but still it didn't work.
The include is not for the linker, but for the compiler; it defines the prototypes of the functions that you use so the compiler can check if you pass incorrect types to it (etc).

Quote:

Originally posted by olspookishmagus
Being more curious I removed the:
Code:

#include <math.h>
line completely and when I typed:
Code:

debian-sarge:~ # gcc -x c angles-sins.c -lm
it compiled again.
Add -Wall to your compile command to switch all warnings on and try again without the include. Quite sure that you will get a warning (although it will still compile)

naf 11-01-2005 09:50 AM

I get no problems:
Code:

#include <math.h>
#include <stdio.h>

int main( int argc, char **argv )
{
    double angle;
    double pi = 4.0 * atan( 1.0 );  /* M_PI is not ANSI C, so calculate a Pi equivalent.  */

    angle = sin( pi / 9.0 );

    fprintf( stdout, "Sin( PI/3 ) = %f\n", angle );

    return 0;
}

Because you are taking an ANSI C class, you should specify -ansi to restrict inclusion to only standard ANSI C definitions.
Code:

ShellPrompt> gcc -x c -Wall -ansi sin-angles.c -lm && a.out
Sin( PI/3 ) = 0.342020

You may want to seperate the compilation from the linking:
Code:

gcc -x c -c -Wall -ansi sin-angles.c
gcc -o sin-angles sin-angles.o -lm



All times are GMT -5. The time now is 12:53 PM.