【问题标题】:Problem compiling self-created header file using Dev-C++?使用 Dev-C++ 编译自创头文件时出现问题?
【发布时间】:2010-12-01 10:14:11
【问题描述】:

我在 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 中编译的完全相同的代码(我使用虚拟机运行 ubuntu,即 vmplayer),它编译时没有错误。

我需要在 Dev-C++ 中设置什么来编译文件吗?

【问题讨论】:

  • 嗨,Karl,您使用什么进行 C/C++ 开发?我是新手,今天刚开始 C/C++。我的讲师不限制我们可以使用什么软件。有什么很酷的 IDE 推荐(最重要的是:“易于编译”)吗?

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


【解决方案1】:

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

【讨论】:

  • 我好多年没用那个东西了,宁愿用cygwin或者msys
  • Cygwin 还是 msys?如果我没记错的话,我在任何代码编辑器中编写C/C++代码,然后打开命令行(windows中的CMD)并编译,是吗?
  • 是的,完全正确;两者都提供 gcc 和 make,尽管 msys 的 gcc 并没有将所有内容都链接到 libcygwin1
  • 谢谢。我将使用 notepad++ 作为代码编辑器和 cygwin 作为编译器 =)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2023-03-31
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多