c语言 - 我导入了一个标头,但它没有导入我的源代码



我试图导入一个模块,但当我构建并运行时,它说:对addNum的未定义引用。这是两个小时,我正在努力寻找为什么有人能帮忙,我是一个初学者。

Main.c:

#include <stdio.h>
#include <stdlib.h>
#include "testFunction.h"
int main()
{
int result = addNum(12);
printf("%d", result);
return 0;
}

testFunction.h:

#ifndef TESTFUNCTION_H_INCLUDED
#define TESTFUNCTION_H_INCLUDED
int addNum(int num);
#endif // TESTFUNCTION_H_INCLUDED

testFunction.c:

#include <stdio.h>
#include <stdlib.h>
#include "testFunction.h"
int addNum(int num){
return num + num;
}

正如@WhozCraig所引用的,很明显,您缺少main.c中的函数定义。因此,在编译main.c之后,没有函数addNum的定义如果编译多个文件是您期待了解的内容。在编译之前,有几种方法可以链接多个文件。

  1. 最基本的方法是使用命令,在本例中为:gcc Main.c testFunction.c -g -Wall
  2. 在这种类型的场景中,您几乎总是需要的最简洁的方法是编写makefile

最新更新