Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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.
 
"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.
 
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;
}
 
The OP stated at he end of the post that they put a main() in the code already and got this result:


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)
 
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
 
one of my problems is that the book gave me a function, and i assumed it was a stand alone program example.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.