LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-03-2006, 09:08 AM   #1
pranavojha
Member
 
Registered: Feb 2006
Location: Bangalore, India
Distribution: Fedora Core 3
Posts: 68

Rep: Reputation: 15
tan(x) function doesn't work in linux C


The "tan()" fucntion in linux doesn't work on my machine. It gives some error along with some message like "ld status 1" (I am right now on the windows machine so can't tell you the exact error).
The problem persists for all the <math.h> functions like"pow(),sin(),cos() atan()" etc.

How can I use the <math.h> header file so that the tan() and other functions will work?.
 
Old 03-03-2006, 09:21 AM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Did you compile as
Code:
gcc -lm program_name.c
 
Old 03-03-2006, 09:22 AM   #3
pranavojha
Member
 
Registered: Feb 2006
Location: Bangalore, India
Distribution: Fedora Core 3
Posts: 68

Original Poster
Rep: Reputation: 15
humm....

Quote:
Originally Posted by Berhanie
Did you compile as
Code:
gcc -lm program_name.c
nope... should i?? wii try...
 
Old 03-03-2006, 11:17 PM   #4
pranavojha
Member
 
Registered: Feb 2006
Location: Bangalore, India
Distribution: Fedora Core 3
Posts: 68

Original Poster
Rep: Reputation: 15
thanx it works!

Quote:
Originally Posted by Berhanie
Did you compile as
Code:
gcc -lm program_name.c
hey thanx that works well but plz tell me why use "gcc" along with "-lm"
 
Old 03-03-2006, 11:33 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"-lm" means "link the standard C math library, 'libm.so', into my executable"
 
Old 03-10-2006, 06:40 AM   #6
tiger9999tiger
LQ Newbie
 
Registered: Mar 2006
Posts: 5

Rep: Reputation: 0
Some more thoughts on this....

Thanks for the answers, it fixed my problem with sin(x) also. But I am wondering if this is correct behaviour. After all, in the program there is an #include <math.h> directive, which I think should cause the correct libraries to be loaded. I suppose that the linker knows nothing about compiler directives. But I do wonder if that is really correct, or should it be fixed?
 
Old 03-10-2006, 07:04 AM   #7
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
The include tells the compiler to use a certain include file. This file contains function prototypes, definitions etc. So the compiler e.g can check if you use the correct syntax for a function.

It does not include the actual functions that you want to use. These are placed in labraries and you (sometimes) have to include the specific library during the linking.
 
Old 03-10-2006, 07:11 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Actually, you always need to tell which libraries your program use, except for the standard c library ([g]libc), which is always linked by default.
 
Old 03-20-2006, 10:52 AM   #9
tiger9999tiger
LQ Newbie
 
Registered: Mar 2006
Posts: 5

Rep: Reputation: 0
OK, I understand that, and how to get round it, and thanks to all who have answered both the original poster and myself. It saved me possibly many hours.

But, it still seems to me that there is a duplication of input required by declaring the library with an #include directive in the source code (which I would definitely expect to do), and also invoking it on the command line. I would have thought that the #include alone "should" be sufficient, which is what I was questioning. After all, what is an #include for?

Now there may well be a very good and fundamental reason for all of this, if so it would be very interesting to know. Otherwise, I would suggest that just maybe a very minor enhancement to the way things work, if it is possible, might please a lot of people, myself included.
 
Old 03-20-2006, 11:17 AM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The header files defines the prototype of the function(s) that you wish to access.

The libraries consist of the actual binary code that will be executed when the program runs.

These are different files, living in different file locations. There may even be different versions of the library on the same machine (for example versions optimised for speed of execution or for size of executable, or a really new bells and whistles version rather than the staid but stable version)

So which one to link in? That is where the switch comes in to tell the linker please link in this version of the library.
 
Old 03-20-2006, 11:57 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Not to mention plenty of different include files can refer to a single library, or a combination of libraries, and you only need to include the ones necessary.

On the other hand, you can have "libraryless" include files, defining useful things for your program, but that are not backed at all by a library.

In summary, there is absolutely no one to one relationship between include files and libraries.
 
Old 03-21-2006, 03:28 AM   #12
tiger9999tiger
LQ Newbie
 
Registered: Mar 2006
Posts: 5

Rep: Reputation: 0
Thank you both for the explanations, which do indeed make sense now.

I must say that support via this site is commendably fast, far better than anything I have ever seen in the proprietary world!
 
Old 03-21-2006, 07:56 AM   #13
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 tiger9999tiger
I must say that support via this site is commendably fast, far better than anything I have ever seen in the proprietary world!
Dang did I forget to copyright my comments...
 
  


Reply

Tags
linking, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
PHP Mail() function doesn't work Messiadbunny Linux - Software 4 06-11-2005 04:40 AM
How does the mail function work(PHP) Alexander.s Programming 1 05-15-2005 09:02 AM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
strcmp function does not seemed to work Linh Programming 4 06-12-2003 06:09 AM
The search function should work MUCH better now. jeremy LQ Suggestions & Feedback 0 07-04-2001 12:30 PM

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

All times are GMT -5. The time now is 11:31 AM.

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