2010-04-14 105 views
2

我正在學習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

我發現有什麼問題。當我在邏輯視圖中時,我正在物理視圖中添加文件。

+0

我有同樣的錯誤,但我不明白你的解決方案,請你解釋一下。 – Kamel 2016-02-16 22:59:52

0

試着改變你的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