Thread View: gnu.gcc.bug
2 messages
2 total messages
Started by root
Tue, 26 Nov 1996 00:00
trouble compiling
Author: root
Date: Tue, 26 Nov 1996 00:00
Date: Tue, 26 Nov 1996 00:00
31 lines
1804 bytes
1804 bytes
Hello... I recently started using the GCC C compiler on my Linux box, and
am having some trouble. I'm trying to use the atan (arc tangent)
function (in math.h), but when I compile my code, the linker (I think)
tells me:
/tmp/cca006221.o(.text+0x20): undefined reference to `atan'
my (test) source code consists of the following:
#include <stdio.h>
#include <math.h>
void main()
{
printf("%f\n",atan(5));
}
Simple, to the point, and, from what I can tell, syntactically correct.
I'm not sure what this message is trying to tell me, but it sounds to me
like it can't find the header file... but I really have no idea. I'd
include the complete -v output of the compile if I could, but I don't
know how to redirect standard error, and I really don't feel like copying
down the whole page of messages. Also, I got a similar message
("undefined reference to <whatever>") when trying to use some of my own
functions defined in separate files. I'm somewhat new to C, so I could
be missing something very obvious to someone else...but I'd appreciate
any help I can get! So... if anyone gets this message, a reply would be
greatly appreciated (did I repeat myself there?). Thanks in advance.
Brian Cochrane
brideb@efn.org
Re: trouble compiling
Author: Alex Martelli
Date: Thu, 28 Nov 1996 00:00
Date: Thu, 28 Nov 1996 00:00
110 lines
4253 bytes
4253 bytes
root@lazarus.blasphuphmus.com (root) writes:
>Hello... I recently started using the GCC C compiler on my Linux box, and
>am having some trouble. I'm trying to use the atan (arc tangent)
>function (in math.h), but when I compile my code, the linker (I think)
>tells me:
>/tmp/cca006221.o(.text+0x20): undefined reference to `atan'
Yes, I think it's the linker, too.
>my (test) source code consists of the following:
>#include <stdio.h>
>#include <math.h>
>void main()
>{
> printf("%f\n",atan(5));
>}
So, you _are_ using atan() -- so the linker is right.
>Simple, to the point, and, from what I can tell, syntactically correct.
>I'm not sure what this message is trying to tell me, but it sounds to me
>like it can't find the header file... but I really have no idea. I'd
Oh, no, it's not that (else the error message would be from the
compiler, not from the linker). What it cannot find is the code
that *implements* atan -- because you haven't told it where to
find it. That "where" is the maths library, libm.a (or however
it's called on your system to indicate it's shared -- libm.so,
or whatever). And how you tell the linker to use a certain
library libWHATEVER.a is by adding a -lWHATEVER switch at the
end of the "compile and link" command line.
>include the complete -v output of the compile if I could, but I don't
>know how to redirect standard error, and I really don't feel like copying
Under sh, or any compatible shell such as bash, 2>afile will
redirect standard error to that file.
>down the whole page of messages. Also, I got a similar message
>("undefined reference to <whatever>") when trying to use some of my own
>functions defined in separate files. I'm somewhat new to C, so I could
Perhaps you were not supplying the correct and complete list of
your .o files on the link command line.
>be missing something very obvious to someone else...but I'd appreciate
>any help I can get! So... if anyone gets this message, a reply would be
>greatly appreciated (did I repeat myself there?). Thanks in advance.
I think your best way to proceed is to do separate compilation and
then linking. Among other things this ensures that you will not
be uncertain of whether an error message is coming from the compiler
or from the linker.
So, first you just compile:
gcc -O -c mytest.c
this leaves you with file mytest.o, if everything worked.
Then, you link:
gcc -o mytest mytest.o -lm
this makes executable file mytest (if it works OK).
If the source in mytest.c is calling a function you have
written in file myfun.c, you will also have done:
gcc -O -c myfun.c
and will link with:
gcc -o mytest mytest.o myfun.o -lm
and so on.
On each of these lines you can add a "2>errors" to redirect
standard error to a file, if you want to examine it at
leisure and/or send it to somebody.
An alternative way to obtain a full transcript of a working
session is:
script thelogfile
(do all your work)
control-D
this will leave in thelogfile a complete recording of all
that has been shown on the tty, both stuff you typed and
responses from any programs you may have run (you may want
to clean thelogfile up because it will also record all sort
of escape sequences, backspaces you used for corrections,
carriage returns at the end of lines, and so on).
Good luck!
Alex
--
DISCLAIMER: these are MY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it -- Phone: +39 (51) 597 313 [Fax: 597 120]
Cad.Lab S.p.A., v. Ronzani 7/29, 40033 Casalecchio (BO), Italia
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads