LinuxQuestions.org
Help answer threads with 0 replies.
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


Closed Thread
  Search this Thread
Old 06-27-2010, 04:26 PM   #1
shridhar005
Member
 
Registered: Jul 2008
Posts: 90

Rep: Reputation: 17
Wink how to connect math.h library to c program


hello friends
I've started programming in c recently . following are the details about scene:


* vim is editor and program is compiled in gcc
* whenever a program involving math function appears it gives error i.e
though math.h is included in header file compiler complains about it
it gives error of unrecognized function that math function like sqrt, etc
* question is how to connect math.h to a program.
 
Old 06-27-2010, 04:34 PM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Quote:
question is how to connect math.h to a program.
-lm
... and reading some documentations...
 
1 members found this post helpful.
Old 06-27-2010, 05:06 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by shridhar005 View Post
hello friends
I've started programming in c recently . following are the details about scene:


* vim is editor and program is compiled in gcc
* whenever a program involving math function appears it gives error i.e
though math.h is included in header file compiler complains about it
it gives error of unrecognized function that math function like sqrt, etc
* question is how to connect math.h to a program.
Enter

C math library

into favorite WEB search engine and read the matches.
 
Old 06-27-2010, 07:33 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
When you compile your program with gcc you will also (typically) invoke the linker as well. For an overview: the compiler converts your source code to object code (it takes something that you can read into something that the computer can read) the compiler does this for each source code file generating one object fiel for each source code file that you have in your program; the linker joins all of this object code together (along with any libraries you are using) to form an executable.

The Maths library alos need to be linked in but it doesn't get linked in by default. So you will need to tell it to link in the maths library, that is where the switch -lm comes into play.

If you have one source code file called myCalc.c, which uses the maths library, and you want your program to be called calc, use the following command:
Code:
gcc -o calc -lm myCalc.c
 
1 members found this post helpful.
Old 06-27-2010, 07:59 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Sergei Steshenko View Post
Enter

C math library

into favorite WEB search engine and read the matches.
Which I did. Ten matches came up:
Two for C++
One for Python
Two for vendor version of Maths libraries

Of the remaining five matches only one mentioned linking but for someone who doesn't understand the linking process its significance would be very easy to miss.

Incidentally, the clearest advice came from a C++ specific page.
Quote:
Compiling with gcc

In order to use some of the above functions, certain versions of the gcc compiler require the math library to be explicitly linked in using the -lm command-line option.
cppreference.com

In summary you advice would have been of very little help to someone who was just stating to learn to program and hadn't yet understood the nuances of gcc.

So please, don't just tell people to use a search engine as if it is a panacea, context is important and often that is what people lack when they come to LinuxQuestions.
 
Old 06-27-2010, 08:49 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by graemef View Post
Which I did. ...
You know what, one more word needs to be added:

C math library linking

- try in Yahoo. Fifth match in my case.

Such questions (requiring only keywords search) are ridiculous and are kinder garden level.

The very first sign of alarm is the "connect" word the OP used. It looks like the OP's problems are much deeper than the math library.
 
Old 06-27-2010, 08:52 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
And

C math library tutorial

yields useful links.
 
Old 06-27-2010, 08:54 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Sergei Steshenko View Post
Such questions (requiring only keywords search) are ridiculous and are kinder garden level.
At one point in time we were all in kindergarten and so such questions are not ridiculous. Just part of the learning process.
 
Old 06-27-2010, 09:15 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by graemef View Post
At one point in time we were all in kindergarten and so such questions are not ridiculous. Just part of the learning process.
Here is hard evidence (just "c math library") - see the attachment, bottom of the picture - the answer is in the picture:

Quote:
You must #include <math.h> and must remember to link in the math library at compilation: cc mathprog.c -o mathprog -lm
I see it as a case of utter laziness.
Attached Thumbnails
Click image for larger version

Name:	math-lm.png
Views:	78
Size:	84.4 KB
ID:	3992  
 
Old 06-28-2010, 05:03 AM   #10
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by Sergei Steshenko View Post
Here is hard evidence (just "c math library") - see the attachment, bottom of the picture - the answer is in the picture:
Yes it is. If you do exactly the right search and know exactly where to look. And the OP is posting here because they do not know those things.


Quote:
Such questions (requiring only keywords search) are ridiculous and are kinder garden level.
I agree with graemef. Please stop perpetually chastising people just because they don't know as much as you. That's why they're here.

Last edited by JohnGraham; 06-28-2010 at 05:05 AM.
 
Old 06-28-2010, 05:17 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JohnGraham View Post
...
because they don't know as much as you. That's why they're here.
Nowadays people know how to use WEB search engines from the(ir) teen age.

The legitimate questions to ask are the ones which can't be easily answered through keyword search.

For example, suppose one has a map of a city with SRC and DST points on it and asks about an algorithm regarding finding the shortest way between the two points. In order to find the algorithm(s) one has to know the "traveling salesman problem" term, which is not obvious. For example, I learned about the term in English less than 10 years ago, though I knew the term in a different language.

In the OP's case plain keyword search yields an answer even without visiting the URLs returned by the search engines (see the attachment in an earlier post of mine).
 
Old 06-28-2010, 09:54 AM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Sergei Steshenko View Post
Nowadays people know how to use WEB search engines from the(ir) teen age.
Going off topic for a moment this assumption is completely incorrect. The global internet penetration is about 26% for more details see Internet World Stats.

Quote:
Originally Posted by Sergei Steshenko View Post
The legitimate questions to ask are the ones which can't be easily answered through keyword search.
As you pointed out earlier the original poster was unaware of the term linking and so by you very definition the question was valid and so didn't warrant you condescending approach.

Last edited by graemef; 06-28-2010 at 09:55 AM.
 
Old 06-29-2010, 06:43 AM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This arguement about how to use searching is not helping the OP, so let's stop now---OK?

Sergei;
You are of course correct about finding things on Google, but less experienced people often do not know how to do this efficiently. I suggest a less confrontational approach.
 
Old 06-29-2010, 08:31 AM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by graemef View Post
...
As you pointed out earlier the original poster was unaware of the term linking and so by you very definition the question was valid and so didn't warrant you condescending approach.
Then he shouldn't be doing "C" programming in the first place (with any library). I.e. the whole thread is invalid - it's like "how do I build a rocket not even bothering to learn physics ?".

To be more particular, the first match Yahoo yields on "gcc tutorial" is http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html .

In the tutorial:

Code:
Frequently used compilation options
...
Compile myprog.C when it contains Xlib graphics routines.

    g++ myprog.C -o myprog -lX11

If "myprog.c" is a C program, then the above commands will all work by replacing g++ with gcc and "myprog.C" with "myprog.c". Below are a few examples that apply only to C programs.

Compile a C program that uses math functions such as "sqrt".

    gcc myprog.C -o myprog -lm
.

Entering into Yahoo "GNU gcc library" as the 5-th match yields http://www.network-theory.co.uk/docs...cintro_79.html , in it :

Code:
...
The short-cut library linking option -l can also be used to link the program, without needing to specify the full filename of the library explicitly:

$ gcc -Wall -L. main.c -lhello -o hello
.

I.e. two very basic keyword searches yield the needed knowledge (both the particular knowledge regarding the math library and the "link" term) if one makes at least the minimal decency effort. I remember I heard at work from a colleague "It takes so much times to read manuals, that's why I'm asking you". And this is really bothering. I.e. the guy didn't want to read the manual even after being told which one to read.

The answer to all such threads should be: "Have you been banned on all WEB search engines ?".
 
1 members found this post helpful.
Old 06-29-2010, 08:47 AM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Then he shouldn't be doing "C" programming in the first place
And there are some people that should not even get out of bed in the morning, n'est-ce pas?

I understand your frustration with people who seem to be too lazy to search for things, but please consider toning it down a bit.

shridhar005;
If you still need help, please start a new thread. Be sure to tell us what Google searches you have done.

Last edited by pixellany; 06-29-2010 at 09:28 AM. Reason: typo
 
  


Closed Thread



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
error: Standard C Math Library not found. banso_boy Linux - Newbie 10 04-24-2008 10:30 AM
need a c or c++ advanced math library ra2000 Linux - Software 1 09-19-2007 11:39 AM
math library issues on ubuntu 5.10 (i think) anon192 Programming 3 05-04-2007 01:36 PM
math program that I can enter math functions ... Four General 5 04-19-2006 08:02 PM
math library for linux kooshball Programming 4 05-28-2005 07:05 PM

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

All times are GMT -5. The time now is 03:32 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