在发布这个问题之前,我已经阅读了数百页的谷歌搜索结果,但无法找到解决此问题的方法。我的程序非常简单-只有以下3个文件:
test.h
输出应该打印“Hello world”,但实际上它一直抛出这个错误:
test.h
#ifndef SYMBOL
#define SYMBOL
#include <stdio.h>
void output(void);
#endif
test.c
#include "test.h"
void output(void)
{
printf("%s\n", "Hello world");
}
untitled.c
#include <stdio.h>
#include "test.h"
void main()
{
output();
return;
}
我使用终端并输入以下命令进行编译:
gcc -o aaa untitled.c
输出应该打印“Hello world”,但实际上它一直抛出这个错误:
Undefined symbols for architecture x86_64:
"_output", referenced from:
_main in untitled-00f904.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我花了整整一天的时间寻找解决方案,尝试了所有可能的建议,但没有一个有效。
有一个值得注意的地方:如果我更改 untitled.c 中包含的文件 (#include "test.h"
变成 #include "test.c"
),编译器就可以正常工作。
请问有人能够解释为什么会这样,并说明如何解决吗?
gcc -Wall -g -v
进行编译,以了解正在发生的情况。使用gcc -v
检查您的gcc
是否符合您的预期。 - Basile Starynkevitchmain()
函数应该返回什么——非Microsoft系统的答案是int
。 - Jonathan Leffler