math.h and pow under Linux - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

math.h and pow under Linux

crvosscrvoss Posts: 49Member
I'm working on a C program under Linux that needs to use pow(). When I include math.h I get a bunch of errors related to the internals in the math.h library along with the message:

undefined reference to 'pow'

What's wrong here?

Comments

  • Joe MayoJoe Mayo Posts: 72Member
    You need to link to the math libraries.

    when you compile use the '-lmath' option.



    : I'm working on a C program under Linux that needs to use pow(). When I include math.h I get a bunch of errors related to the internals in the math.h library along with the message:
    :
    : undefined reference to 'pow'
    :
    : What's wrong here?
    :


  • crvosscrvoss Posts: 49Member
    That cleared up many of the problems but still comes back with: undefined reference to 'pow'

    How do I fix that?

    : You need to link to the math libraries.
    :
    : when you compile use the '-lmath' option.
    :
    :
    :
    : : I'm working on a C program under Linux that needs to use pow(). When I include math.h I get a bunch of errors related to the internals in the math.h library along with the message:
    : :
    : : undefined reference to 'pow'
    : :
    : : What's wrong here?
    : :
    :
    :

  • Joe MayoJoe Mayo Posts: 72Member
    I don't see why you are getting that error "undefined reference to 'pow'" :(
    Linux has a number of compilers you can use. Try these compilers

    cc -lmath for mainly c programs
    or
    gcc -lmath for c programs
    or
    g++ -lmath for c and c++ programs

    and if your still having problems I'm stumped. I've used
    'pow(x,y)' on linux and it worked fine as long as I linked to the math libraries.



    : I'm working on a C program under Linux that needs to use pow(). When I include math.h I get a bunch of errors related to the internals in the math.h library along with the message:
    :
    : undefined reference to 'pow'
    :
    : What's wrong here?
    :


  • crvosscrvoss Posts: 49Member
    Do I use the -lmath as a linker argument or a compiler argument?

    : I don't see why you are getting that error "undefined reference to 'pow'" :(
    : Linux has a number of compilers you can use. Try these compilers
    :
    : cc -lmath for mainly c programs
    : or
    : gcc -lmath for c programs
    : or
    : g++ -lmath for c and c++ programs
    :
    : and if your still having problems I'm stumped. I've used
    : 'pow(x,y)' on linux and it worked fine as long as I linked to the math libraries.
    :
    :
    :
    : : I'm working on a C program under Linux that needs to use pow(). When I include math.h I get a bunch of errors related to the internals in the math.h library along with the message:
    : :
    : : undefined reference to 'pow'
    : :
    : : What's wrong here?
    : :
    :
    :

  • Joe MayoJoe Mayo Posts: 72Member
    I don't understand your question. But I'm gonna guess 'use the -lmath option as a compiler argument'. If it's a 'c' program use

    cc -lmath or gcc -lmath

    'cc' and 'gcc' are linux compilers. The '-lmath' is an option to tell the compiler which libraries to link with. 'g++' is a 'c/c++' compiler. Used the same way

    g++ -lmath

    If your having trouble then type "man gcc" or "man g++" at the linux prompt. This will give you info on the options for those compilers. (warning the man pages on those compilers are quite long, you might want to redirect output to a file)



    : Do I use the -lmath as a linker argument or a compiler argument?
    :
    : : I don't see why you are getting that error "undefined reference to 'pow'" :(
    : : Linux has a number of compilers you can use. Try these compilers
    : :
    : : cc -lmath for mainly c programs
    : : or
    : : gcc -lmath for c programs
    : : or
    : : g++ -lmath for c and c++ programs
    : :
    : : and if your still having problems I'm stumped. I've used
    : : 'pow(x,y)' on linux and it worked fine as long as I linked to the math libraries.
    : :
    : :
    : :
    : : : I'm working on a C program under Linux that needs to use pow(). When I include math.h I get a bunch of errors related to the internals in the math.h library along with the message:
    : : :
    : : : undefined reference to 'pow'
    : : :
    : : : What's wrong here?
    : : :
    : :
    : :
    :


  • crvosscrvoss Posts: 49Member
    Thanks for your help. I found that the compiler parameter must be -lm and include math.h. There is still a warning:

    in file included from .../math.h:350, from main.c:5:
    .../mathinline.h: In function '__sgn1l':
    .../mathinline.h:469: warning: traditional C rejects automatic aggregate initialization

    Do you have any idea what might be causing that?

    : I don't understand your question. But I'm gonna guess 'use the -lmath option as a compiler argument'. If it's a 'c' program use
    :
    : cc -lmath or gcc -lmath
    :
    : 'cc' and 'gcc' are linux compilers. The '-lmath' is an option to tell the compiler which libraries to link with. 'g++' is a 'c/c++' compiler. Used the same way
    :
    : g++ -lmath
    :
    : If your having trouble then type "man gcc" or "man g++" at the linux prompt. This will give you info on the options for those compilers. (warning the man pages on those compilers are quite long, you might want to redirect output to a file)


  • Joe MayoJoe Mayo Posts: 72Member
    I'm stumped! I've never seen that b4.



    : Thanks for your help. I found that the compiler parameter must be -lm and include math.h. There is still a warning:
    :
    : in file included from .../math.h:350, from main.c:5:
    : .../mathinline.h: In function '__sgn1l':
    : .../mathinline.h:469: warning: traditional C rejects automatic aggregate initialization
    :
    : Do you have any idea what might be causing that?
    :
    : : I don't understand your question. But I'm gonna guess 'use the -lmath option as a compiler argument'. If it's a 'c' program use
    : :
    : : cc -lmath or gcc -lmath
    : :
    : : 'cc' and 'gcc' are linux compilers. The '-lmath' is an option to tell the compiler which libraries to link with. 'g++' is a 'c/c++' compiler. Used the same way
    : :
    : : g++ -lmath
    : :
    : : If your having trouble then type "man gcc" or "man g++" at the linux prompt. This will give you info on the options for those compilers. (warning the man pages on those compilers are quite long, you might want to redirect output to a file)
    :
    :


  • smart_heartsmart_heart Posts: 1Member
    [color=Orange]Hello Hi,
    Hum wat to do? i have used #include
    but still i am not getting output to pow(2,3)??

    My suggestions:
    1) try to use mathematical formula i.e.,
    2^3.
    2) try "gcc program_name.c -lm" (without ""),
    because -lm try to link math.h header to the program u
    have written.
    3)becarefull while printing i.e., %f %d varies ur results.
    eg:
    int k=3;
    printf("%f",pow(k,k))=> this may give u 0.000000.
    but, printf("%d",pow(k,k)) will do fine!!! => 27.
    [/color]
  • salecst4salecst4 Posts: 4Member

    SHENZHEN CREATIVE SKY TECHNOLOGY CO., LTD

    1. compatibility: PC, Netbook or Maciontosh with USB port

    2.Operation System: Windows vista/xp/2000/ME/98/95 or Mac. OS9.1 and above

    3.Drivers: Required only for Windows 98/95, online download available

    4.Transfer rate: High speed 2.0 USB

    5. Read and Write speed: 3 to 5 MB/S

    6. printed logo: 4 colours

    7. Free for logo design

    8. Warranty: 5 years

    9.Payment: T/T and Western Union

    10.Delivery time: 3 to 5 days after confirmed the payment

    11.Delivery mothod: DHL. UPS and so on

Sign In or Register to comment.