【问题标题】:keep getting this error in this system "linker command failed with exit code 1"在此系统中不断收到此错误“链接器命令失败,退出代码为 1”
【发布时间】:2019-03-28 03:58:04
【问题描述】:

我正在制作一个菜单系统,用户输入一定数量的数字,系统输出平均值和总和,然后显示用户输入系统但我无法获得的数字到目前为止,我收到此错误消息“clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)”。

#include <stdio.h>
#include <stdlib.h>
#define LIMIT 1000
#define PAUSE system("pause")

//prototype functions

int getChoice();
int getNumbers(int numbers[], int eSize);
void displayAllNumbers(int array[], int eSize);
void displayAverage(int numbers[], int eSize);
void showSum(int numbers[], int eSize);

int main(){

int choice;
int numbers[LIMIT] = {0};
int eSize = 0;

do{
    choice = getChoice();
    switch(choice){
        case 1: //get a bunch of numbers from user
            eSize = getNumbers(numbers, eSize);
            break;
        case 2: //show the sum of the user-entered numbers
            showSum( numbers, eSize);
            break;
        case 3: //show the average of the user-entered numbers
            displayAverage(numbers, eSize);
            break;
        case 4: //show the numbers
            displayAllNumbers(numbers , eSize);
            break;
        case 5: //quit the program
            printf("thank you for using my program!\n");
            PAUSE;
            break;
        default:
            printf("invalid choice... please try again!\n");
            PAUSE;
            break;
    }//end of switch

}while(choice != 5);
//end of dowhile


}//end of main```

【问题讨论】:

  • 完整的错误信息是什么?
  • "_displayAllNumbers",引用自:arrayIntroLuisDuarte-9d0673.o 中的_main "_getChoice",引用自:arrayIntroLuisDuarte-9d0673.o 中的_main,引用自:arrayIntroLuisDuarte-9d0673.o 中的_main “_showSum”,引用自:arrayIntroLuisDuarte-9d0673.o ld 中的 _main:未找到架构 x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

标签: c function switch-statement


【解决方案1】:

当编译器找不到程序中使用的函数定义时,通常会发生链接器错误。 您可能没有编译其他源文件。 if函数定义:

int getChoice();
int getNumbers(int numbers[], int eSize);
void displayAllNumbers(int array[], int eSize);
void displayAverage(int numbers[], int eSize);
void showSum(int numbers[], int eSize);

在另一个文件中说functions.cmain.c 包含main() 函数,那么您必须将它们编译为clang main.c functions.c &lt;other options&gt; -o &lt;output file&gt;

【讨论】:

    【解决方案2】:

    您显示的源文件仅包含main() 的定义。 getChoice() 等其他辅助函数只是声明了,当前文件中缺少定义。如果这些帮助函数的定义在其他文件中,那么也应该编译这些文件。

    假设main()包含在main.c中并且辅助函数在helper.c中,那么在gcc中编译它们的命令是

    gcc main.c helper.c -o executive_name
    

    解决这个问题的更好方法是将所有函数原型(又名声明)放在一个头文件中,比如 helper.h,并在 main.chelper.c 中包含帮助文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 2015-03-08
      • 2017-04-12
      • 2018-06-12
      • 1970-01-01
      相关资源
      最近更新 更多