compiling C program containing math functions

H

happytoday

Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out


The program :
*----------------
#include "stdio.h"
#include "math.h"


void main()
{
int angle_degree;
double angle_raduis,pi,result;

// sin=pi*(angle_degree) / 100.0 ;
// pi=4.0 * atan (1.0);


pi=4.0 * atan (1.0);

printf (" angle \t sin \n");
printf (" ----- \t --- \n");
while (angle_degree <= 360 )
{
result=sin(pi*angle_degree)/180.0;
printf (" %3d \t %f \n",angle_degree , result);
angle_degree=angle_degree+10;
}
}
 
K

Kenny McCormack

Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out


The program :
*----------------
#include "stdio.h"
#include "math.h"


void main()

Nice work! Well done.

--
No, I haven't, that's why I'm asking questions. If you won't help me,
why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.
 
B

bartc

happytoday said:
Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out


The program :
*----------------
#include "stdio.h"
#include "math.h"

I tried this; there's a couple of changes:

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

int main(void)
{
int angle_degree=0;
double angle_raduis,pi,result;

// sin=pi*(angle_degree) / 100.0 ;
// pi=4.0 * atan (1.0);


pi=4.0 * atan (1.0);

printf (" angle \t sin \n");
printf (" ----- \t --- \n");
while (angle_degree <= 360 )
{
result=sin(pi*angle_degree/180.0);
printf (" %3d \t %f \n",angle_degree , result);
angle_degree=angle_degree+10;
}
}

As for the linking error, I seem to remember with gcc or some such compiler,
you have to use a special switch, perhaps -lm to include the maths library.
 
E

Ersek, Laszlo

<28bdfc76-9921-46ab-bbc7-a5714a3a88f2@z11g2000yqz.googlegroups.com>,
Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out

[snip]

http://www.opengroup.org/onlinepubs/9699919799/utilities/c99.html

----v----
-l m
This option shall make available all interfaces referenced in <math.h>
[...] An implementation may search this library in the absence of this
option.
----^----

Nice work! Well done.

Nice work! Well done.

lacos
 
A

Andrew Poelstra

Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out


The program :
*----------------
#include "stdio.h"
#include "math.h"

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

is more idiomatic and will probably help your compiler
search the right places before searching the wrong ones.
void main()

main() returns int. You are pulling a fast one on your
operating system with this, a dangerous thing indeed.
{
int angle_degree;

Please indent properly.
double angle_raduis,pi,result;

// sin=pi*(angle_degree) / 100.0 ;
// pi=4.0 * atan (1.0);

Please don't use // comments on usenet. They often wrap
around, and some people here use compilers which do not
support them.
pi=4.0 * atan (1.0);

printf (" angle \t sin \n");
printf (" ----- \t --- \n");
while (angle_degree <= 360 )
{
result=sin(pi*angle_degree)/180.0;

I'm pretty sure this is not what you want. Perhaps if
you used whitespace sanely you would see your mistake.
printf (" %3d \t %f \n",angle_degree , result);
angle_degree=angle_degree+10;

There is a shorthand for this:
angle_degree += 10;

Your original problem is because gcc only checks libc, but
the math functions are in libm. To compile your program
correctly, try

gcc prog.c -lm

But first you should fix the other errors.
 
K

Kenny McCormack

<28bdfc76-9921-46ab-bbc7-a5714a3a88f2@z11g2000yqz.googlegroups.com>,
Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out

[snip]

http://www.opengroup.org/onlinepubs/9699919799/utilities/c99.html

----v----
-l m
This option shall make available all interfaces referenced in <math.h>
[...] An implementation may search this library in the absence of this
option.
----^----

Nice work! Well done.

Nice work! Well done.

lacos

You didn't get it, did you?

--
No, I haven't, that's why I'm asking questions. If you won't help me,
why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.
 
K

Keith Thompson

happytoday said:
Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out
[...]

Several people have answered your question, but I don't think anyone has
mentioned that it's in the FAQ, <http://www.c-faq.com/>, question 14.3.
 
E

Ersek, Laszlo

You didn't get it, did you?

("You didn't get it, did you?")


I'm not sure anymore. Here's your non-snipped reply:

----v----
From (e-mail address removed) Mon Apr 26 17:18:56 2010
Date: Mon, 26 Apr 2010 15:18:56 +0000 (UTC)
From: Kenny McCormack <[email protected]>
Newsgroups: comp.lang.c
Subject: Re: compiling C program containing math functions

Though I included math.h I got that error :

Undefined first referenced
symbol in file
sin sine.o
atan sine.o
ld: fatal: Symbol referencing errors. No output written to a.out


The program :
*----------------
#include "stdio.h"
#include "math.h"


void main()

Nice work! Well done.

--
No, I haven't, that's why I'm asking questions. If you won't help me,
why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.

----^----

I (still) think you were mocking the OP for:
- stdio.h and math.h placed between quotation marks, not angle brackets,
- main() returning void, not int,
- missing function parameter declarations in main().

I interpreted your "Nice work! Well done." not only as "do your homework
first" but even as "you're an idiot for not doing your homework first".
You were unhelpful and arrogant. (Quite amusingly, you seem to deride
precisely this behavior in your sig.) As such, your contribution seemed to
merit the same treatment.

If I misunderstood something, then I apologize.

Cheers,
lacos
 
K

Kenny McCormack

Ersek said:
I (still) think you were mocking the OP for:
- stdio.h and math.h placed between quotation marks, not angle brackets,
- main() returning void, not int,
- missing function parameter declarations in main().

I interpreted your "Nice work! Well done." not only as "do your homework
first" but even as "you're an idiot for not doing your homework first".
You were unhelpful and arrogant. (Quite amusingly, you seem to deride
precisely this behavior in your sig.) As such, your contribution seemed to
merit the same treatment.

OK - just this one time - and because you're (relatively) new around
here - I'll explain it.

The post is obvious troll bait. The stuff about missing "-lm" is just
red herring. The point of the post is the "void main" - everything else
flows from that. That's why I snipped the post at "void main" and
commented solely on that. As I say, that's the whole point of OP's post.

I was congratulating (quite seriously - and with no irony) the OP for a
good job of trolling. As we know from long experience, nothing gets the
regs more riled up than "void main".
If I misunderstood something, then I apologize.

No problem.

--
(This discussion group is about C, ...)

Wrong. It is only OCCASIONALLY a discussion group
about C; mostly, like most "discussion" groups, it is
off-topic Rorsharch [sic] revelations of the childhood
traumas of the participants...
 
E

Ersek, Laszlo

The post is obvious troll bait. The stuff about missing "-lm" is just
red herring. The point of the post is the "void main" - everything else
flows from that. That's why I snipped the post at "void main" and
commented solely on that. As I say, that's the whole point of OP's
post.

Oh. Thanks for the explanation. The post seemed (at a cursory glance)
genuine. (Perhaps this increases its trolling quality in your view.) I've
been co-trolled then. I wasted a few seconds on finding that reference.
I'm not happy. Even though my contributions (or rather, my intent to
contribute) has been minuscule recently, it seems I have to tune it down
even more. Not very inspiring.

lacos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top