29

Doing 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. How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hendrik
  • 301
  • 1
  • 3
  • 7

7 Answers7

12

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

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

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

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

For an integer number I have made a function:

#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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Punit Vara
  • 3,744
  • 1
  • 16
  • 30
3

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 useful

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rusty
  • 168
  • 6
1

You can define the power function

int power(int m, int n) {
    // Implement the function body
}

And then your issue will get fixed.

You're getting an error, because there isn't any deceleration for the defined function. So add the deceleration as shown above.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ANjaNA
  • 1,404
  • 2
  • 16
  • 29
  • function Declarations has absolutely nothing to do with linking. Even if you omit every kind of declarations, and compile without `-Werror`, and supply all the libraries as linker flags, it will link just fine. Declarations fir functions are just a new thing(they were nonexistant before ansiC, and only made mandatory in c99 which nobody enables during compilation) which helps compiler to do some accounting beforehand. Only type declarations are a must. – Aftnix Dec 04 '15 at 04:55
  • Tried that ANjaNA but no success. However, when I read K&R again it actually said that pow(x,y) was held in the standard library. Again, that did not work immediately, but the following program did. – Hendrik Dec 04 '15 at 05:03
  • 1
    #include double pow(double m, double n); int main() { double i; for (i=0; i<5; ++i) printf("%.0f %.0f %.0f\n", i, pow(2,i), pow(-3,i)); return 0; } – Hendrik Dec 04 '15 at 05:08
  • It's not looking right but the answer was using Double value instead of Int values. I don't know why exactly, but I'm happy (sort of) – Hendrik Dec 04 '15 at 05:10
  • Thank you everyone. If I even become one quarter as smart as you lot I will be a happy man. Yes, I left out the second part of the program because it was on the next page and I didn't realise it was required. Well I do now. – Hendrik Dec 05 '15 at 05:30
1

There is a standard library function which does just that...

#include <math.h>
double pow(double x, double y)

You have to link it explicitly because the default linker, that is ld invoked when no other options given, doesn't link with standard math library.

So you have to do it like...

gcc file.c -lm

Aftnix
  • 4,461
  • 6
  • 26
  • 43
  • Re *"the default linker, that is ld"*: For GCC only, presumably? The signature for the (real - not executable `gcc` on macOS which is ***Clang*** by default) GCC is *"ld returned 1 exit status"* whereas it is *"linker command failed with exit code 1"* for Clang (in this question). – Peter Mortensen Jan 18 '23 at 23:29
  • Using executable 'gcc' would [***invoke Clang*** on that system](https://stackoverflow.com/questions/38840601/how-can-i-ignore-an-error-when-using-gcc-compile-option-werror#comment130977909_38840601), not GCC (also fairly clear from the error message: *"clang: error: linker command..."*). – Peter Mortensen Jan 18 '23 at 23:39
0

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. There is missing {} at the end of the definition and even if you are putting it at the end of the definition, you will get an error because you are not returning nothing for a int function. So, at least if you want to define a function, you have to proceed like this:

int power(int m, int n){return 0};

Then, you will be able to use your function power() in the main function. But, you are doing nothing in the power() function, so you will get nothing back of calling it. If you want to compute the power of a number, you can use the function pow() that is present in the cmath library. A straightforward way of doing it is something like this:

#include <stdio.h>  // printf
#include <cmath>    // pow()
#include <iostream> // cout

void main()
{
    for (int i = 0; i < 10; i++)
        std::cout << i << " " << pow(2, i) << " " << pow(-3, i) << std::endl;
}

I included the iostream to use a different way of printing out using the object cout defined in the std namespace. Note that the pow() function has some requirements for its definition, so be careful with the types you are using. You can take a look at pow for more details.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wayland700
  • 48
  • 8
0

The function should be completed. I met the same question today only because I didn’t define function main in my code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mcdonjuan
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 17 '22 at 01:40
-1

Try command s(save) before clanging. Had the same problem, now it works