错误:体系结构x86_64的未定义符号:....链接器问题

时间:2020-02-07 17:00:14

标签: c

我正在尝试将来自operations.c的函数放入主文件。我为此编写了头文件,并声明其包含在主文件中,但现在看来我的链接器有问题。

主文件(trials.c):

 #include <stdio.h>
 #include <math.h>
 #include "operations.h"


int main()
{

  menuDifInt();

  return 0;
 }

具有功能代码的operations.c文件:

 void menuDifInt()
 {
 }

最后是用于操作的头文件(operations.h):

 #ifndef OPERATIONS_H
 #define OPERATIONS_H

 double trapezoidalrule();
 double fsquare(double x);
 void menuDifInt();

 #endif

我使用以下命令进行编译:

$ gcc trials.c

出现的错误是:

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

2 个答案:

答案 0 :(得分:5)

我使用以下命令进行编译:$ gcc trial.c和$。/ a.out

您不会向编译器提供有关menuDifInt定义位置的任何信息。您还必须传递定义功能的.c文件。仅执行#include "operations.h"并不会自动将operations.c添加到要编译的文件中。

使用以下代码编译代码:

$ gcc trials.c operations.c

答案 1 :(得分:0)

您需要一起编译C文件:

clang -o trials trials.c operations.c

添加所需的任何编译器标志。

请检出诸如make之类的构建工具,以在复杂时简化/自动化构建。

相关问题