【问题标题】:Make Error: Undefined symbols for architecture x86_64:出现错误:架构 x86_64 的未定义符号:
【发布时间】:2015-08-18 03:46:44
【问题描述】:

我刚刚开始使用 C 语言进行编程,并决定使用 make 来构建我的示例应用程序。使用 GCC 命令编译文件可以正常工作,但是使用 make 失败并出现错误。如果 ma​​ke 只是在后台执行 gcc 命令,我不明白为什么这是可能的。

以下是文件列表:

makefile.make:

app.o: app.c helper.h
    gcc -c app.c

helper.o: helper.h helper.c
    gcc -c helper.c

app: app.o helper.o
    gcc app.o helper.o -o app

app.c

/*
 * file: app.c
 */
#include "helper.h"

int main() {
    do_something();
    return 0;
}

helper.h

/*
 * helper.h
 */
void do_something();

helper.c

/*
 * file: helper.c
 */
#include <stdio.h>
#include "helper.h"

void do_something() {
    printf("Test\n");
    printf("Testsss\n");
}

问题:运行“make app”会引发错误消息

Undefined symbols for architecture x86_64:
  "_do_something", referenced from:
      _main in app.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [app] Error 1

我尝试过的有效方法:

  • 使用 gcc 手动编译和链接(使用来自 make 串行执行)
  • 使用“make app.o”和“make helper.o”然后运行“gcc app.o helper.o -o app”可以正确编译和链接应用程序

【问题讨论】:

  • 你试过make -stdlib=libstdc++ -lstdc++吗?
  • 你使用 gcc 还是 clang?从您的错误看来,您至少与 clang 相关联。如果在链接过程中包含 -v 标志会得到什么?
  • 这只是用“GCC”编译的“C”。我是否正确假设 make 没有做任何特别的事情,它只是执行命令来构建东西。 “app.o”和“helper.o”有效,但“app”无效。我很确定“app”只是使用 gcc 的简单链接:“gcc app.o helper.o -o app”。令人沮丧的是“gcc app.o helper.o -o app”工作得很好。
  • 好吧,我觉得问这个问题很愚蠢。我该如何关闭这个? (@Ayseeyou 的回答是正确的)
  • 要使用具有非标准名称的 make 文件,请输入:'make -f filename target'

标签: c gcc makefile x86-64


【解决方案1】:

将“makefile.make”重命名为“makefile”或“Makefile”。 “app.o”和“helper.o”似乎可以工作,但我认为那只是做它的默认指令。

【讨论】:

  • 不知何故,崇高的文本神奇地将“.make”附加到我的 makefile - 重命名并且现在运行良好
  • 还可以将-f makefile.make 提供给make
猜你喜欢
  • 2016-02-02
  • 2017-02-15
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 2014-05-14
  • 2016-05-31
  • 2016-12-12
  • 1970-01-01
相关资源
最近更新 更多