Find the answer to your Linux question:
Results 1 to 7 of 7
First off, I am not a programmer; I am taking a problem solving class and we program in C. We do all of our programming with MS Visual Studio. I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2002
    Posts
    45

    including library functions using gcc


    First off, I am not a programmer; I am taking a problem solving class and we program in C. We do all of our programming with MS Visual Studio. I am used to writing code and hitting the compile&link button. For example, I can compili&link this with no problems:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    main&#40;&#41;
    &#123;    
        int i=5, j=0;
    
        j = pow&#40;i,2&#41;;
        printf&#40;"J = %i", j&#41;;
    
    &#125;
    When I try to compile the same code using Linux and gcc, gcc gives me an error message saying that pow() is undefined. What am I doing wrong?

  2. #2
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    Indeed, this is odd. I've just checked /usr/include/math.h and it has no reference to function pow at all. What version of gcc are you using?
    The best things in life are free.

  3. #3
    Just Joined!
    Join Date
    Oct 2002
    Posts
    45

    problem solved (sort of)

    Not that I claim to know what I am doing, but did you see the comment near the beginning of <math.h> that says the function defintions are in another file? I am not certain the pow() is actually there either. The actual function is not really important; we could use sqrt() for the sake of discussion. I had the bright idea of checking /usr/doc/Linux-HOWTO and found a GCC howto that I must have missed. The howto mention the -l (lower case L)arguement, which includes a specific library. So, I recompiled with gcc -lm -o file file.c(m is the math library; I didn't know that either. see man nm) which works great. But I don't understand why I need to specifically include libraries...

  4. #4
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    In my case, I think the problem is that there are more than one gcc/g++ and libraries installed. I don't think it's being resolved correctly when the compiler reads math.h.

    What distribution are you using? Can you quickly check what versions and library files you have relating to C?
    The best things in life are free.

  5. #5
    Just Joined!
    Join Date
    Oct 2002
    Posts
    45
    I am using Slack 9.1. I am school right now, but from the Slack website...
    • GNU C Library, version 2.3.2.
      GCC 3.2.3

    If you would like the versions of specific files, let me know...

  6. #6
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    How could I have been so stupid.... we are dealing with code in C not C++. In C, I believe that libraries such as math must be loaded using the -l option as you suggested. In C++, I never had to do this and this is where the confusion came about.
    The best things in life are free.

  7. #7
    Just Joined!
    Join Date
    Nov 2003
    Posts
    7

    Re: problem solved (sort of)

    Quote Originally Posted by flaminghyundai
    Not that I claim to know what I am doing, but did you see the comment near the beginning of <math.h> that says the function defintions are in another file? I am not certain the pow() is actually there either. The actual function is not really important; we could use sqrt() for the sake of discussion. I had the bright idea of checking /usr/doc/Linux-HOWTO and found a GCC howto that I must have missed. The howto mention the -l (lower case L)arguement, which includes a specific library. So, I recompiled with gcc -lm -o file file.c(m is the math library; I didn't know that either. see man nm) which works great. But I don't understand why I need to specifically include libraries...
    You always have to specify libraries you want to link to. This is the case for both gcc and Microsoft C (if you ever compiled with their command line compiler cl.exe). Some libraries are linked by default (like libc), while others have to be specified manually.

    Compiler (or better to say linker) CAN NOT and WILL NOT guess which "pow" you want - the one from math library that calculate the power or maybe the one from mylib library that outputs "i'm the greates" or anything else...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •