gcc - Undefined references 
Author Message
 gcc - Undefined references

Hello:
        I can't seem to get math functions referenced on my gcc 2.7.2
compiler running on Linux. Since I was the person who installed the
software, I wonder if my compiler is broken or the problem is more my
inexperience in C. When I compile the attached demo program, I get the
following:
/root/math.c:13: undefined reference to `log'
/root/math.c:15: undefined reference to `exp'
My command line was: gcc -g -lm -o math.o math.c
I thought the math library would be linked by using -lm above. Any help
for a novice would be appreciated. The libraries have been ldconfig-ed.
________________________________________________________________________________
        Larry W. Roberts                                SeaTac, WA.

        http://www.*-*-*.com/ ~lroberts     http://www.*-*-*.com/

________________________________________________________________________________

[ math.c < 1K ]
#include <stdio.h>
#include <math.h>
main()
{      
double exp(double x);

double log(double y);

double x, y, c;      

x = 10;
y = 100;
c = log(y);
printf("The natural log of 100 is %lf", c);
c = exp(x);
printf("\nThe value of e^10 is %lf", c);

Quote:
}



Sat, 17 Apr 1999 03:00:00 GMT  
 gcc - Undefined references

Quote:


> >       I can't seem to get math functions referenced on my gcc 2.7.2
> > compiler running on Linux. Since I was the person who installed the
> > software, I wonder if my compiler is broken or the problem is more my
> > inexperience in C. When I compile the attached demo program, I get the
> > following:
> > /root/math.c:13: undefined reference to `log'
> > /root/math.c:15: undefined reference to `exp'
> > My command line was: gcc -g -lm -o math.o math.c
> > I thought the math library would be linked by using -lm above. Any help
> > for a novice would be appreciated. The libraries have been ldconfig-ed.

> This may seem silly but perhaps you forgot:
> #include <math.h>
> Your linker is telling you that it cannot find the code to use those
> functions.

The *correct* answer -- as is almost inevitable -- is to be found in
the C-FAQ-list. In this case, refer to item 14.3.

(BTW, please eschew the quoting of a multi-line sig when posting a
followup).

--
=============================================================================

     linux for fun, M$ for $$$...and the NFL for what really counts!
=============================================================================



Sat, 17 Apr 1999 03:00:00 GMT  
 gcc - Undefined references

This may seem silly but perhaps you forgot:
#include <math.h>

Your linker is telling you that it cannot find the code to use those
functions.



Quote:
> Hello:
>    I can't seem to get math functions referenced on my gcc 2.7.2
> compiler running on Linux. Since I was the person who installed the
> software, I wonder if my compiler is broken or the problem is more my
> inexperience in C. When I compile the attached demo program, I get the
> following:
> /root/math.c:13: undefined reference to `log'
> /root/math.c:15: undefined reference to `exp'
> My command line was: gcc -g -lm -o math.o math.c
> I thought the math library would be linked by using -lm above. Any help
> for a novice would be appreciated. The libraries have been ldconfig-ed.

____________________________________________________________________________
____
Quote:
>    Larry W. Roberts                                SeaTac, WA.

>    http://www.oz.net/~lroberts     http://lroberts.oz.net


____________________________________________________________________________
____


Sat, 17 Apr 1999 03:00:00 GMT  
 gcc - Undefined references

I renamed math.c to arith.c just to avoid a possible confusion suggested
by someone else with the include file math.h . Then:
lroberts:~# gcc -g  -o arith.o arith.c -lm
/tmp/cca001781.o: In function `main':
/root/arith.c:13: undefined reference to `log'
/root/arith.c:15: undefined reference to `exp'

As you see, there isn't any difference in the outcome even with the -lm at
the end. Thanks for the FAQ suggestion. I'll see if I can find it.
________________________________________________________________________________
        Larry W. Roberts                                SeaTac, WA.

        http://www.oz.net/~lroberts     http://lroberts.oz.net

________________________________________________________________________________


Quote:


> >/root/math.c:13: undefined reference to `log'
> >/root/math.c:15: undefined reference to `exp'
> >My command line was: gcc -g -lm -o math.o math.c
> >I thought the math library would be linked by using -lm above. Any help

> Didn't anyone tell you that -lm must be mentioned AFTER the names of modules
> which use the math library? It's safest to put it at the end of your compiler
> command line.

> In any case, you should have read the comp.lang.c FAQ list; I'm sure
> this is in there.



Sat, 17 Apr 1999 03:00:00 GMT  
 gcc - Undefined references



Quote:
>/root/math.c:13: undefined reference to `log'
>/root/math.c:15: undefined reference to `exp'
>My command line was: gcc -g -lm -o math.o math.c
>I thought the math library would be linked by using -lm above. Any help

Didn't anyone tell you that -lm must be mentioned AFTER the names of modules
which use the math library? It's safest to put it at the end of your compiler
command line.

In any case, you should have read the comp.lang.c FAQ list; I'm sure
this is in there.



Sat, 17 Apr 1999 03:00:00 GMT  
 gcc - Undefined references


|>   This message is in MIME format.  The first part should be readable text,
|>   while the remaining parts are likely unreadable without MIME-aware tools.

|>
|> --1036932765-789891942-846583923=:269
|> Content-Type: TEXT/PLAIN; CHARSET=US-ASCII

|>
|> Hello:
|>   I can't seem to get math functions referenced on my gcc 2.7.2
|> compiler running on Linux. Since I was the person who installed the
|> software, I wonder if my compiler is broken or the problem is more my
|> inexperience in C. When I compile the attached demo program, I get the
|> following:
|> /root/math.c:13: undefined reference to `log'
|> /root/math.c:15: undefined reference to `exp'
|> My command line was: gcc -g -lm -o math.o math.c
|> I thought the math library would be linked by using -lm above. Any help
|> for a novice would be appreciated. The libraries have been ldconfig-ed.

Close, but you're putting -lm in the wrong place.  Gcc searches libraries
in the order they're indicated on the command line, so the way you've
typed the command, gcc searches libm.so BEFORE math.c is compiled,
so no external references are resolved, then math.c is compiled and there's
no libm around to resolve the references (gcc doesn't "remember" the -lm
before math.c).  What you want to do is put the switch *after* the code
you're compiling, i.e.

gcc -g math.c -lm

kent

(P.S., why are you naming the executable math.o?  That name implies
that math.o is the object file corresponding to math.c, but what you're
doing is compiling AND linking math.c.  If you want to generate the object
file for math.c, you can do gcc -g -c math.c,  here -lm is irrelevant because
math.c is not linked.)

|> ________________________________________________________________________________
|>   Larry W. Roberts                                SeaTac, WA.

|>   http://www.oz.net/~lroberts     http://lroberts.oz.net

|> ________________________________________________________________________________
|>
|> --1036932765-789891942-846583923=:269
|> Content-Type: TEXT/PLAIN; CHARSET=US-ASCII; NAME="math.c"
|> Content-Transfer-Encoding: BASE64

|> Content-Description:
|>
|> I2luY2x1ZGUgPHN0ZGlvLmg+DQojaW5jbHVkZSA8bWF0aC5oPg0KbWFpbigp
|> DQp7ICAgICAgIA0KZG91YmxlIGV4cChkb3VibGUgeCk7DQogICAgICAgICAg
|> ICAgIA0KZG91YmxlIGxvZyhkb3VibGUgeSk7DQoNCmRvdWJsZSB4LCB5LCBj
|> OyAgICAgIA0KDQp4ID0gMTA7DQp5ID0gMTAwOw0KYyA9IGxvZyh5KTsNCnBy
|> aW50ZigiVGhlIG5hdHVyYWwgbG9nIG9mIDEwMCBpcyAlbGYiLCBjKTsNCmMg
|> PSBleHAoeCk7DQpwcmludGYoIlxuVGhlIHZhbHVlIG9mIGVeMTAgaXMgJWxm
|> IiwgYyk7IA0KfQ0K
|> --1036932765-789891942-846583923=:269--



Sat, 17 Apr 1999 03:00:00 GMT  
 gcc - Undefined references


:
:       I can't seem to get math functions referenced on my gcc 2.7.2
: compiler running on Linux. Since I was the person who installed the
: software, I wonder if my compiler is broken or the problem is more my
: inexperience in C. When I compile the attached demo program, I get the
: following:
: /root/math.c:13: undefined reference to `log'
: /root/math.c:15: undefined reference to `exp'
: My command line was: gcc -g -lm -o math.o math.c
: I thought the math library would be linked by using -lm above. Any help
: for a novice would be appreciated. The libraries have been ldconfig-ed.

You are right that -lm links the math library. Unfortunately it is
important where in your command line it appears. Command line options
for the linker have to be placed at the very end of the command line.
Simply try "gcc -g -o math.o math.c -lm". Calling your executable file
"math.o" does not seem to be a very good idea, because UNIX (and Linux)
uses the .o suffix for object files which are generated during compiling.
Generating an executable "math.o" from an object file "math.o" might
result in some trouble.

- Jochen

--
Jochen Schoof   (http://www-info2.informatik.uni-wuerzburg.de/staff/joscho)
___________________________________________________________________________
\_Address __/  Informatik II, Uni Wuerzburg, Am Hubland, D-97074 Wuerzburg



Sat, 17 Apr 1999 03:00:00 GMT  
 gcc - Undefined references


Quote:
>I renamed math.c to arith.c just to avoid a possible confusion suggested
>by someone else with the include file math.h .

No possible confusion here.

Quote:
>Then:
>lroberts:~# gcc -g  -o arith.o arith.c -lm

                        ^^^^^^^
As several people pointed out, using the .o "extension" on an executable
file is both confusing and plain stupid.

Quote:
>/tmp/cca001781.o: In function `main':
>/root/arith.c:13: undefined reference to `log'
>/root/arith.c:15: undefined reference to `exp'

>As you see, there isn't any difference in the outcome even with the -lm at
>the end.

It's hard to tell what you're doing wrong or what's wrong with your
installation.  Both log and exp work just fine on Linux:

    ues12:~/tmp 58> uname -a
    Linux ues12 1.2.13 #4 Sat Oct 7 22:32:36 MET 1995 i486
    ues12:~/tmp 59> cat test.c
    #include <stdio.h>
    #include <math.h>

    main()
    {
        printf("%f\n", log(1.0) + exp(0.0));
        return 0;
    }
    ues12:~/tmp 60> gcc test.c -lm
    ues12:~/tmp 61> ./a.out
    1.000000

My suggestion: go to linux support newsgroup.  Your problem has nothing
to do with the topic of this newsgroup: the C programming language.

Quote:
>Thanks for the FAQ suggestion. I'll see if I can find it.

Nobody removed the FAQ from the "standard" places, it's still there.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 18 Apr 1999 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. linking gcc with gprof: undefined symbol

2. undefined symbol caused by gcc

3. undefined reference to 'main'

4. Help with "Undefined reference" error

5. Undefined reference to function 'sqrt()'

6. undefined reference to `pow'

7. undefined reference to setsockopt, inet_addr, socket, htons

8. undefined reference to `_msize'

9. undefined reference to cos

10. undefined reference

11. Undefined Reference Error

12. undefined reference to

 

 
Powered by phpBB® Forum Software