编译器抛出错误"未定义符号"在C程序中

时间:2014-11-12 06:22:51

标签: c macos unix compiler-errors header-files

我在test.c

中有以下内容
#include <stdio.h>
#include "stdfn.h"

int main(void)
{
    printOut(10, "Hello, world!\n");
    //bash("say hello world");
}

来源在这里: https://github.com/KrasnayaSecurity/HydroCarbon

我在尝试编译时遇到此错误:

President-JohnHEden:HydroCarbon aleksandr$ gcc test.c -o test -Wall
Undefined symbols for architecture x86_64:
  "_printOut", referenced from:
      _main in test-134873.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 :(得分:0)

此处,您的链接器无法找到printOut()函数的定义。

您已通过printOut()包含了test.c文件中stdfn.h的声明,但test.c中没有定义fpr该功能。该定义出现在stdfn.c中。因此,您需要将两个文件一起编译以生成二进制文件。

将编辑行更改为

gcc test.c stdfn.c -o test -Wall

答案 1 :(得分:0)

你可以做一个

gcc test.c stdfn.c -o test -Wall 

(假设stdfn.c是printOut()所在的位置),以便将包含用户定义函数的文件链接到当前使用它的文件。