使用Dev-C ++编译自创头文件时出现问题?

时间:2010-12-01 10:14:12

标签: c compilation header-files dev-c++

我在windows vista上使用Dev-C ++。我有3个文件位于同一目录中。他们是:

- math_functions.h
- math_functions.c
- test3.c

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;
}

test3.c代码:

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

main ()
{
  int   theSum     = sum (8, 12);
  float theAverage = average (16.9, 7.86, 3.4);

  printf ("the sum is: %i ", theSum);
  printf ("and the average is: %f \n", theAverage);
  printf ("average casted to an int is: %i \n", (int)theAverage);
}

无法编译。我得到的错误信息是:

C:\Users\eSum\AppData\Local\Temp\ccKmdaaa.o(.text+0x3a) In function `main': 
[Linker error] undefined reference to `sum' 
[Linker error] undefined reference to `average' 
C:\Users\eSum\AppData\Local\Temp\ccKmdaaa.o(.text+0x3a) ld returned 1 exit status 

我在ubuntu中使用完全相同的代码编译(我使用虚拟机i运行ubuntu,例如vmplayer),它编译时没有错误。

我是否需要在Dev-C ++中设置任何内容来编译文件?

2 个答案:

答案 0 :(得分:0)

Dev-C ++似乎没有将math_function.c与test3.c链接起来制作text3.exe这是Dev-C ++中的配置问题,你很可能需要将math_function.c添加到Dev-C ++项目中

答案 1 :(得分:0)

问题不在于头文件,而在于您的项目设置。您必须将math_functions.c添加到项目中,以便编译并与test3.c链接。

相关问题