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


Reply
  Search this Thread
Old 05-09-2005, 06:57 PM   #1
davidleroux1
Member
 
Registered: Oct 2004
Location: columbus oh
Distribution: mepis
Posts: 109

Rep: Reputation: 15
c and math functions


using k develop (and new to c programing and kdevelop)

I wrote a program with the following two lines of code
I get the following errors. I wrote the "log" and "exp" as
math functions and I do have a line of code at the top stating

#include <math.h>



loc1 = (- 10.795) * (1.0 - loc1) - 2.1835 * log(loc1) + 2.2195;
loc1 = 29.9 * 1.0 / exp(2.3025 * loc1);


/home/david/enthalpy/src/enthalpy.c:94: undefined reference to `log'
enthalpy.o(.text+0x2d3):/home/david/enthalpy/src/enthalpy.c:95: undefined reference to `exp'



why are these math functions not working
 
Old 05-09-2005, 07:26 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
When you compile, you need to add "-lm" to the command line whenever you use functions from the math library. I don't know how you would do this with Kdevelop, but that's likely what your problem is.
 
Old 05-09-2005, 08:11 PM   #3
davidleroux1
Member
 
Registered: Oct 2004
Location: columbus oh
Distribution: mepis
Posts: 109

Original Poster
Rep: Reputation: 15
now get parse error

now i get a parse error whenever i add "-lm" or just -lm
anywher in the code

any good tutorials on c programming in linux
 
Old 05-09-2005, 09:03 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
You misunderstand. The "-lm" does not go in your code. It is an option to give to the compiler. Kdevelop should have some sort of setting or menu somewhere that you can give extra options when the code is compiled. I don't use Kdevelop, so I can't say where that menu is. You'll have to hunt for it, look in the Kdevelop documentation, or wait for someone to come by and say where it is.

As for C tutorials, just do a search on Google. I'd actually go to www.google.com/linux and search for "C tutorial" or something along those lines. There should be tons of them out there. Or you can pick up a book. There are tons of those out there too
 
Old 05-09-2005, 10:25 PM   #5
davidleroux1
Member
 
Registered: Oct 2004
Location: columbus oh
Distribution: mepis
Posts: 109

Original Poster
Rep: Reputation: 15
environment variable?

I found a place for project options with

configure, run, and make options.
lots of places to put things but does anyone know?


would it be easyer to compile from command interpreter
eg. gcc -lm program-file

new to using linux compilers

Last edited by davidleroux1; 05-09-2005 at 10:28 PM.
 
Old 05-09-2005, 10:36 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
would it be easyer to compile from command interpreter
eg. gcc -lm program-file
To be honest, that's the way I program... emacs and the command line.

Contents of simple_prog.c:
Code:
#include <stdio.h>
#include <math.h>

int main( int argc, char *argv[] )
{
  double loc1;

  loc1 = 0.5;

  loc1 = (- 10.795) * (1.0 - loc1) - 2.1835 * log(loc1) + 2.2195;
  loc1 = 29.9 * 1.0 / exp(2.3025 * loc1);

  fprintf( stdout,
           "loc1 = %f\n",
           loc1 );

  return 0;
}
Compile command:
Code:
 gcc -lm -o simple_prog simple_prog.c
And run the result like so:
Code:
 ./simple_prog
 
Old 05-09-2005, 11:11 PM   #7
davidleroux1
Member
 
Registered: Oct 2004
Location: columbus oh
Distribution: mepis
Posts: 109

Original Poster
Rep: Reputation: 15
thanks i'll try it that way

I'll try it with out using k develop
 
Old 05-10-2005, 06:28 AM   #8
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
I believe -lm will have to go at the end of your gcc command for the linker to pick it up, or it will be mistaken for a gcc option.
 
Old 09-14-2006, 09:47 PM   #9
fabianrios
Member
 
Registered: Apr 2005
Location: Atlanta
Distribution: FC3
Posts: 34

Rep: Reputation: 15
Red face ??????

For Kdevelop go to:

Project>Project Options>Configure Options>C>Compiler flags

Add -lm in the compiler flags
 
  


Reply



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
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
functions in math.h philgar Slackware 3 03-27-2005 05:08 PM
error while accessing math functions in kernel modules dypgrp Programming 0 01-19-2005 09:12 AM
c compiler won't recognize math functions dreamgoat Red Hat 2 08-16-2004 05:45 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

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

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