Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get clang: error: linker command failed with exit code 1?

Tags:

I am a newbie who is slowly working his way through K&R using Xcode. In the Functions section I entered the code for their example of the power function as follows.

#include <stdio.h>  int power(int m, int n);  int main() { int i;  for (i=0; 1<10; ++i)     printf("%d %d %d\n", i, power(2,i), power(-3,i));  return 0; } 

When I try to run it the following error appears:

Undefined symbols for architecture x86_64:   "_power", referenced from:       _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

I have read a lot of the answers to this question but cannot see how it applies to my situation with such a little program.
Would be grateful for any help.

like image 548
Hendrik Avatar asked Dec 04 '15 04:12

Hendrik


People also ask

What does clang error linker command failed with exit code 1 mean?

This means that you linked to something that clang can't find. Can you post your code so that we can find the error? It is most-likely a function prototype or #include that is causing the issue.

What does clang error mean?

First you get the error because the compiler can not find the definition of the power function that you are using. Even if you write int power(int m, int n); There is an error because you are not defining the function.


2 Answers

Why do I get clang: error: linker command failed with exit code 1?

You just declared the function. There is not any definition in code. At the time of linking process , compiler(here clang) cannot link power function to its definition so linker throws the error in this kind of situation. If you define

int power(int x, int y)            {                 \*do calculation*/          } 

Then linker can link your declaration of power function to its definition ,you will not get any error.

For integer number I have made function for you.

#include <stdio.h> int power(int base, int exp); int main() { int i;  for (i=0; i<10; ++i)     printf("%d %d %d\n", i, power(2,i), power(-3,i));  return 0; }  int power(int base, int exp) {     int result = 1;     while (exp)     {         if (exp & 1)             result *= base;         exp >>= 1;         base *= base;     }      return result; } 

Compile this with gcc file.c

Hope you understand the function. Good luck :-)

like image 133
Punit Vara Avatar answered Nov 02 '22 15:11

Punit Vara


You missed the definition of function int power (int base,int n) which is given after your main ends on the next page of the book.

When you declare prototype of a function you need to define what it should do you just declared the power function and never defined that, that's why you got error.

Include the following definition, your code will compile the way you wants it to be.

int power (int base,int n){ int i,p; p=1; for (i=1;i<=n;++i) p=p*base; return p; } 

PRE-EDIT ANSWER NOW THIS IS NOT RELEVANT BUT USEFULL

I think you want to use function pow() defined in math.h.

double pow(double a, double b) 

The C library function pow(double a, double b) returns a raised to the power of b. This function returns a double value so to print that correct specifier will be "%lf".

In this case you just need to include header file

#include<math.h> 

In your program.

There is no need to give function declaration int power(int m, int n);

The error you are having is due to giveing I as on of the parameter to pow() because when you will compile your code (after including math.h and using pow() replacing i with any integer numbers will compile your code and will give proper output.

printf("%lf %lf %lf\n", i, pow(2,3), pow(3,2)); 

This will give you proper result but when you compile it with

for (i=0; i<10; ++i){ printf("%lf %lf %lf\n", i, pow(2,i), pow(-3,i)); } 

It throws same error so I think pow() takes only constants as input so it won't run in for loop.

And if you don't want to include math.h you can simply declare

extern double pow (double base, double exponent);  

That will link correctly with the library code without using the math.h include file, here is an example.

int main() { extern double pow (double base, double exponent); printf("%lf",pow( 8.0, 8.0 )); return 0; }  

For more on pow() you can check man page on Linux i.e. man pow.

like image 34
Rusty Avatar answered Nov 02 '22 14:11

Rusty