【问题标题】:Unknown Objective-C error "Undefined symbols for architecture x86_64," everything seems OK未知的 Objective-C 错误“架构 x86_64 的未定义符号”,一切似乎都正常
【发布时间】:2012-07-16 09:10:05
【问题描述】:

我正在尝试按照这里的教程进行操作:

http://cocoadevcentral.com/articles/000081.php

当我到达“头文件”部分时,在 Mac OSX 命令行中运行 gcc test1.c -o test1 后,我不断收到一条奇怪的错误消息:

Undefined symbols for architecture x86_64:
  "_sum", referenced from:
      _main in ccdZyc82.o
  "_average", referenced from:
      _main in ccdZyc82.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

math_functions.h:

int sum(int x, int y);
float average(float x, float y, float z);

math_functions.c:

int sum(int x, int y) {
  return x + y;
}

float average(float x, float y, float z) {
  return (x + y + z)/3;
}

最后,我的 test1.c:

#include <stdio.h>
#include "math_functions.h"

main() {
  int thesum = sum(1, 2);
  float ave = average(1.1, 2.21, 55.32);

  printf("sum = %i\nave = %f\n(int)ave = %i\n", thesum, ave, (int)ave);
}

我似乎正确地遵循了一切,但我不明白该错误来自何处。帮忙?

【问题讨论】:

标签: objective-c ios


【解决方案1】:

您有两个单独的源文件,math_functions.c 和 test1.c,它们都需要编译并链接在一起。错误消息告诉您编译器没有找到函数averagefloat,这是因为它们来自math_functions.c,而您只编译了test1.c。

您链接到的示例告诉您输入:

gcc test3.c math_functions.c -o test3

【讨论】:

  • 我知道!我所要做的就是向下滚动一些。
【解决方案2】:

您没有在包含sum()average() 函数的目标文件中进行链接。

这样做:

$ gcc -c -o math_functions.o math_functions.c
$ gcc -c -o test1.o test1.c
$ gcc -o test1 test1.o math_functions.o

前两行将源文件编译为目标文件,最后一行将目标文件链接到可执行文件中。

您需要花一些时间来学习 make,因为没有开发人员会费心输入那么多内容来编译(在您知道之前,您的文件名错误并编译了您的源文件!)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2015-10-27
    • 2015-03-01
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多