Thread: Undefined Symbol error

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    Undefined Symbol error

    This is my test prog n my code goes like this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main() {
    
    	double x, y, z;
    	double num = 5;
    	
    	x = log(num);
    	y = exp(num);
    	z = pow(num, (double)2);
    
    	printf("\n\nlog of num = %e\nexp of num = %e\npow of num = %e\n\n", x, y, z);
    
    	return 0;
    
    }
    However i got these errors while compling my prog in unix.

    ld: 0711-317 ERROR: Undefined symbol: .pow
    ld: 0711-317 ERROR: Undefined symbol: .log
    ld: 0711-317 ERROR: Undefined symbol: .exp

    I haved checked my <math.h> library and it has these functions.
    Can anyone help to explain what this error means?

    Thks

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    6
    are you using gcc or g++?

    make sure to link the math lib too with "-lm". for example if your source file is test.c:

    gcc test.c -lm

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    thks.
    That works for my single file. I am using xlc by the way.

    However for my makefile, how should i go about adding that -lm?
    What does that -lm represent?

    My makefile code goes lik this:
    Code:
    ##
    ##
    ## "Test" makefile
    ##
    ##
    
    trial: test.o readinput.o configuring.o functions.o saveoutput.o 
    	xlc -o trial test.o readinput.o configuring.o functions.o saveoutput.o
    
    test.o: test.cpp variables.h complex.h
    	xlc -c test.cpp -lm
    readinput.o: readinput.cpp variables.h complex.h
    	xlc -c readinput.cpp -lm
    configuring.o: configuring.cpp variables.h complex.h
    	xlc -c configuring.cpp -lm
    functions.o: functions.cpp variables.h complex.h
    	xlc -c functions.cpp -lm
    saveoutput.o: saveoutput.cpp variables.h complex.h
    	xlc -c saveoutput.cpp -lm
    What is wrong?

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    6
    the "-l" is a flag for the compiler telling it to link something. i'm guessing the "m" is for the math library.

    try putting an "-lm" in "trial". example:

    trial: test.o readinput.o configuring.o functions.o saveoutput.o
    xlc -o trial test.o readinput.o configuring.o functions.o saveoutput.o -lm

    i think you only need to link the math lib when you are linking all those object files, not during the compilation of your source files (you could probably remove the "-lm" elsewhere).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM