power problem

Discussion in 'Mac Programming' started by jamesapp, May 23, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    james-collinss-macbook-pro:chap1 jamescollins$ gcc power1.c -o power1.out
    Undefined symbols:
    "_main", referenced from:
    start in crt1.10.5.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    got the following error message when i tried to compile a program from a book.
    here is my test program which i called power1.c:

    Code:
    /* power: raise base to n-th power; n >= 0; version 2 */ 
    int power(int base, int n)
    {
      int p;
      
      for (p = 1; n > 0; --n)
          p = p * base;
      return p;
    }
    
    this was how the program appeared in the book.
    one thing i noticed was the line

    Code:
    int power(int base, int n)
    
    shouldn't this line end in a semi-colon?
    when i added a semi-colon and recompiled the program i got the following from terminal:

    james-collinss-macbook-pro:chap1 jamescollins$ gcc power1.c -o power1.out
    power1.c:3: error: syntax error before ‘{’ token

    another thing i will mention is there is no main
    i read from the book
    "Normally you are at liberty to give functions whatever names you like, but "main" is special-your program begins executing at the beginning of main. This means that every program must have a main somewhere."

    when i added main to the program i got the following error messages

    james-collinss-macbook-pro:chap1 jamescollins$ gcc power1.c -o power1.out
    power1.c:3: error: syntax error before ‘main’
    power1.c: In function ‘main’:
    power1.c:7: error: ‘n’ undeclared (first use in this function)
    power1.c:7: error: (Each undeclared identifier is reported only once
    power1.c:7: error: for each function it appears in.)
    power1.c:8: error: ‘base’ undeclared (first use in this function)

    any help would be appreciated.
     
  2. tacoman667 macrumors regular

    Joined:
    Mar 27, 2008
    #2
    "int power(int base, int n)" is a function and thus the enclosing {} take place of the semicolon. The error you are seeing is probably because that function was not put into the corresponding ".h" file.
     
  3. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #3
    You didn't include a main() function is the problem:

    Code:
    #include <stdio.h>
    
    int main() {
    
    int myPower = power(10, 3);
    
    printf("Power: %d\n", myPower);
    
    return 0;
    
    }
    
    /* power: raise base to n-th power; n >= 0; version 2 */ 
    int power(int base, int n)
    {
      int p;
      
      for (p = 1; n > 0; --n)
          p = p * base;
      return p;
    }
     
  4. tacoman667 macrumors regular

    Joined:
    Mar 27, 2008
    #4
    The OP stated at he end of the post that they put a main() in the code already and got this result:


     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I'm not sure what the context is in the book. I copied and pasted you code and ran:
    gcc -c power.c

    And it built the object file without issue. The -c option means not to link. It seems that your problem was that gcc was trying to link and generate an executable, which is not possible without a main function. It appears you tried to correct this by changing your function signature to main from power, but then the parameters are not valid anymore (n and base).

    If you want to test, sayer posted an example that will build, link and run, and it will test the power function.

    -Lee
     
  6. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #6
    one of my problems is that the book gave me a function, and i assumed it was a stand alone program example.
     
  7. ert3 macrumors 6502a

    ert3

    Joined:
    Dec 10, 2007
    #7
    You really have to read carefully when you copy out of the book.

    I once copied almost a whole damn program from a book and then realized I forgot to include all of the ";" in my Objective-C application.

    took me forever to figure out why the downloaded example worked and mine did not.
     

Share This Page