蛋疼的extern和error LNK2001: unresolved external symbol

編程過程中總會遇到一些小問題,有些是大意引起的,越是着急越找不到問題所在;有些是因爲有一些小知識點你不知道引起的。總之,有時找不到問題所在真的很蛋疼。。。


        這篇小短文要說的是因爲extern和源文件後綴名(.cpp ? .c)引起的 連接錯誤“error LNK2001: unresolved external symbol "int test_test" (?test_test@@3HA)”, 開發環境爲Visual Studio 2005 + Windows XP。 

       vs2005建立了console空工程之後,添加了下面文件:

  1、main.cpp

#include "test.h"
void main()
{
test_test = 2;
}

2、test.h

#ifndef test_h
#define test_h
extern int test_test;
#endif


3、test.c

#include "test.h"
int test_test = 123;


【編譯】時,就出現下面的錯誤:

1>main.obj : error LNK2001: unresolved external symbol "int test_test" (?test_test@@3HA)


      剛開始找了很久就發現問題,真的很蛋疼,後臺乾脆早點去吃飯了,回來跟之前代碼中用到extern的代碼對比了一下。突然想到了,可能是因爲源文件後綴名引起的,修改test.c爲test.cpp,一些OK了,蛋也不疼了。於是寫出來,希望能幫到其他朋友。

    (”error LNK2001: unresolved external symbo“這個錯誤有很多種原因引起的,這只是一種情況。另外也沒有在Visual Studio的其他版本或其他編譯器試過)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章