LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2005, 04:13 AM   #1
olspookishmagus
LQ Newbie
 
Registered: Dec 2004
Posts: 10

Rep: Reputation: 0
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?
 
Old 11-01-2005, 05:27 AM   #2
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 728

Rep: Reputation: 33
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?
 
Old 11-01-2005, 05:37 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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)
 
Old 11-01-2005, 09:50 AM   #4
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
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
 
  


Reply

Tags
include, programming



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
math library for linux kooshball Programming 4 05-28-2005 07:05 PM
not able to include archived C library into my program gauravsahni Programming 4 11-15-2004 02:30 AM
#include <math.h> does not work enlight1 Mandriva 2 05-29-2004 07:59 PM
How to include library directories? davee Programming 7 04-05-2004 05:41 AM
include a library in program calble Programming 1 10-09-2003 12:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration