C语言 为什么我会收到 clang: error: linker command failed with exit code 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34080846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:15:10  来源:igfitidea点击:

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

c

提问by Hendrik

I'm 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.

我是一个新手,正在使用 Xcode 慢慢地通过 K&R 工作。在函数部分,我输入了他们的幂函数示例的代码,如下所示。

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

体系结构 x86_64 的未定义符号:“_power”,引用自:main.o ld 中的 _main:找不到体系结构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

I've 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.

我已经阅读了这个问题的很多答案,但看不出它如何适用于我这样一个小程序的情况。
将不胜感激任何帮助。

采纳答案by Punit Vara

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

为什么我会收到 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 powerfunction to its definition so linker throws the error in this kind of situation. If you define

您刚刚声明了该函数。代码中没有任何定义。在链接过程中,编译器(此处为 clang)无法将power函数链接到其定义,因此在这种情况下链接器会抛出错误。如果你定义

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

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

然后链接器可以将你的power函数声明链接到它的定义,你不会得到任何错误。

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

编译这个 gcc file.c

Hope you understand the function. Good luck :-)

希望你了解这个功能。祝你好运 :-)

回答by Rusty

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.

您错过int power (int base,int n)了本书下一页主要结束后给出的函数定义。

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 ANSWERNOW THIS IS NOT RELEVANT BUT USEFULL

现在预编辑答案这不相关但有用

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

我想你想使用的功能pow()定义math.h

double pow(double a, double b)

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

C 库函数pow(double a, double b)返回ab. 此函数返回一个双精度值,以便打印正确的说明符将是"%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);

无需给出函数声明 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.hand using pow()replacing iwith any integer numbers will compile your code and will give proper output.

您遇到的错误是由于将 I 作为参数提供给,pow()因为您何时编译代码(在包含math.h并使用任何整数pow()替换后i,将编译您的代码并提供正确的输出。

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 constantsas input so it won't run in forloop.

它抛出相同的错误,所以我认为pow()只需要常量作为输入,所以它不会在for循环中运行。

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

如果你不想包含math.h你可以简单地声明

extern double pow (double base, double exponent); 

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

这将在不使用math.h包含文件的情况下与库代码正确链接,这是一个示例。

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.

有关更多信息,pow()您可以查看 Linux 上的手册页 ie man pow

回答by ANjaNA

you can define powerfunction

你可以定义power函数

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

then your issue will get fixed.

那么你的问题就会得到解决。

Your getting error, because there is not deceleration for the defined function. So add the deceleration as shown in above.

您收到错误,因为定义的函数没有减速。所以添加减速,如上所示。

回答by Aftnix

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 ldinvoked when no other options given, doesn't link with standard math library.

您必须显式链接它,因为默认值linkerld在没有给出其他选项时调用)不与标准数学库链接。

So you have to do it like...

所以你必须像...

gcc file.c -lm

gcc file.c -lm

回答by wayland700

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 函数返回任何内容。所以,至少如果你想定义一个函数,你必须像这样继续:

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 :

然后,您将能够在主函数中使用您的函数 power()。但是,您在 power() 函数中什么也没做,因此调用它不会得到任何回报。如果要计算数字的幂,可以使用 cmath 库中的函数 pow()。一个简单的方法是这样的:

#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 http://www.cplusplus.com/reference/cmath/pow/for more details.

我包含了 iostream 以使用在 std 命名空间中定义的对象 cout 使用不同的打印方式。请注意, pow() 函数对其定义有一些要求,因此请注意您使用的类型。您可以查看http://www.cplusplus.com/reference/cmath/pow/了解更多详细信息。