在Windows上使用MinGW编译NetBeans 6.8中的C/C++项目

问题描述:

我正在学习C,因为VC++ 2008不支持C99功能我刚刚安装了NetBeans并将其配置为与MinGW配合使用。我可以编译单个文件项目(main.c)并使用调试器,但是当我将新文件添加到项目时,出现错误“未定义的引用...该函数(代码)在该文件中..”。很明显,MinGW没有链接我的文件,或者我不知道如何正确地将它们添加到我的项目中(c标准库文件工作正常)。在Windows上使用MinGW编译NetBeans 6.8中的C/C++项目

/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf 
make[1]: Entering directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7' 
/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_7.exe 
make[2]: Entering directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7' 
mkdir -p dist/Debug/MinGW-Windows 
gcc.exe  -o dist/Debug/MinGW-Windows/cppapplication_7 build/Debug/MinGW-Windows/main.o 
build/Debug/MinGW-Windows/main.o: In function `main': 
C:/Users/don/Documents/NetBeansProjects/CppApplication_7/main.c:5: undefined reference to `X' 
collect2: ld returned 1 exit status 
make[2]: *** [dist/Debug/MinGW-Windows/cppapplication_7.exe] Error 1 
make[2]: Leaving directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7' 
make[1]: *** [.build-conf] Error 2 
make[1]: Leaving directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7' 
make: *** [.build-impl] Error 2 
BUILD FAILED (exit value 2, total time: 1s) 

的main.c

#include "header.h" 

int main(int argc, char** argv) 
{ 
    X(); 
    return (EXIT_SUCCESS); 
} 

header.h

#ifndef _HEADER_H 
#define _HEADER_H 
#include <stdio.h> 
#include <stdlib.h> 

void X(void); 

#endif 

由source.c

#include "header.h" 
void X(void) 
{ 
    printf("dsfdas"); 
} 
+1

我们需要确切的错误信息和编译器在我们提供帮助之前抱怨的代码。应该指出的是,MinGW缺少对几个大型Windows API的支持。也许这就是你遇到的问题。 – 2010-04-14 14:23:19

我发现有什么问题。当我在逻辑视图中时,我正在物理视图中添加文件。

+0

我有同样的错误,但我不明白你的解决方案,请你解释一下。 – Kamel 2016-02-16 22:59:52

试着改变你的include防范名

#ifndef _HEADER_H //These 
#define _HEADER_H 
#include <stdio.h> 
#include <stdlib.h> 

void X(void); 

#endif 

以下划线(_)开头的名称保留供C和C++标准库使用。完全有可能_HEADER_H已经在某个地方定义了,这会使main.c无法编译。

+0

这不是问题。当我有例如main.c文件和source.c文件时,我在main.c中包含source.c一切正常。但是当我有main.c-> include-> header.h和source.c-> include header.h时,我得到错误“undefined reference to function'X'” – dontoo 2010-04-14 14:59:17

+0

显然我不知道如何正确添加文件 – dontoo 2010-04-14 15:00:37