2009-12-22 32 views
2

編譯時,我收到以下錯誤消息,但我確定我有在外部Project2.c文件中聲明的變量。有人能給我提示我做錯了什麼嗎?謝謝錯誤'extern'使用 - 無法解析的外部符號

1>main.obj : error LNK2001: unresolved external symbol _num_days 
1>main.obj : error LNK2019: unresolved external symbol _countDays referenced in function _testIt 
1>main.obj : error LNK2001: unresolved external symbol _what_birthday 
1>main.obj : error LNK2001: unresolved external symbol _birth_day 
1>main.obj : error LNK2001: unresolved external symbol _birth_month 
1>main.obj : error LNK2001: unresolved external symbol _birth_year 

的源代碼:

#include <stdio.h> 

/* the following five variables are defined inside project2.c */ 
extern int birth_year; 
extern int birth_month; 
extern int birth_day; 
extern int what_birthday; 
extern int num_days; 

void countDays(void); // a declaration of your function 

int num_tests_passed = 0; 

void testIt(int test_num, 
     int year, int month, int day, int bday, int expected) { 
    birth_year = year; 
    birth_month = month; 
    birth_day = day; 
    what_birthday = bday; 
    countDays(); 
    printf("your answer to test %d was %d, should have been %d\n", 
     test_num, num_days, expected); 

    /* check the correctness and quality of their solution */ 
    if (num_days == expected) { 
     if ((birth_year == year) && (birth_month == month) 
       && (birth_day == day)) { 
      num_tests_passed += 1; 
     } else { 
      printf("you got the answer correct," 
       "but you don't follow instructions very well\n"); 
     } 
    } else { 
     printf("oops.\n"); 
    } 
} 


int main(void) { 
    /* test #1: Born Feb 4, 1964 and about to turn 41 */ 
    testIt(1, 1964, 2, 4, 41, 41 * 365 + 11); // feb 29ths in '64, 68, 72, 
          // 76, 80, 84, 88, 92, 96, 00, and 04 (11 days) 

    /* test #2: Born Jan 22, 1998 and about to turn 2 */ 
    testIt(2, 1998, 1, 22, 2, 2 * 365); // no leap days 

    /* test #3: Born March 22, 1998 and about to turn 2 */ 
    testIt(3, 1998, 3, 22, 2, 2 * 365 + 1); // feb 29, 2000 

    /* test #4: Born March 22, 1898 and about to turn 2 */ 
    testIt(4, 1898, 3, 22, 2, 2 * 365); // no leap days 

    /* test #5: Born Jan 22, 1996 and about to turn 4 */ 
    testIt(5, 1996, 1, 22, 4, 4 * 365 + 1); // feb 29, 1996 

    /* test #6: Born March 22, 1996 and about to turn 4 */ 
    testIt(6, 1996, 3, 22, 4, 4 * 365 + 1); // feb 29, 2000 

    /* test #7: Born March 22, 1796 and about to turn 4 */ 
    testIt(7, 1796, 3, 22, 4, 365 * 4); // no leap days 

    /* test #8: Born Jan 1, 1800 and about to turn 101 */ 
    testIt(8, 1800, 1, 1, 101, 101 * 365 + 24); // There are 26 years divisible by 4 
        // between 1800 and 1901, all of these except 1800 and 1900 are leap years 
        // so there are 24 leap days 
    /* test #9: born Jan 1, 1900 and about to turn 100 */ 
    testIt(9, 1900, 1, 1, 100, 100 * 365 + 24); // 26 years divisible by 4 
        // 1900 was not a leap year, and the 100th brithday comes 
        // before Feb 29, 2000, so only 24 leap days. 


    if (num_tests_passed == 9) { 
     printf("all tests passed successfully. Well done!\n"); 
    } else { 
     printf("you only passed %d of the 9 tests. Looks like you've got some work to do\n", 
      num_tests_passed); 
    } 
} 
+1

我們需要查看鏈接命令。 – 2009-12-22 00:37:15

+0

您可以將此C文件編譯爲目標文件(.o文件),但不是可執行文件,除非將其鏈接到其他必需的文件。 – Artelius 2009-12-22 00:40:31

回答

8

在你的project2.c中,確保你定義了這些變量,並且該文件確實是你的解決方案的一部分。如果源文件被忽略,鏈接器將會因爲找不到定義而發出抱怨。

通常情況下,最好的做法是將這些變量放入一個頭文件並定義。

對於一個簡單的例子,稱這個頭文件project2.h

/* project2.h */ 
extern int birth_year; 
extern int birth_month; 
extern int birth_day; 
extern int what_birthday; 
extern int num_days; 

這裏,按照上的例子,這裏是你的代碼省略了extern變量和到位的#include代替。讓我們也把這個文件稱爲myproject1.c

#include &lt;stdio.h&gt; 
#include "project2.h" 

void countDays(void); // a declaration of your function 

int num_tests_passed = 0; 

void testIt(int test_num, 
     int year, int month, int day, int bday, int expected) { 
    birth_year = year; 
    birth_month = month; 
    birth_day = day; 
    what_birthday = bday; 
    countDays(); 
    printf("your answer to test %d was %d, should have been %d\n", 
     test_num, num_days, expected); 

    /* check the correctness and quality of their solution */ 
    if (num_days == expected) { 
     if ((birth_year == year) && (birth_month == month) 
         && (birth_day == day)) { 
       num_tests_passed += 1; 
     } else { 
       printf("you got the answer correct," 
         "but you don't follow instructions very well\n"); 
     } 
    } else { 
     printf("oops.\n"); 
    } 
} 


int main(void) { 
    /* test #1: Born Feb 4, 1964 and about to turn 41 */ 
    testIt(1, 1964, 2, 4, 41, 41 * 365 + 11); // feb 29ths in '64, 68, 72, 
               // 76, 80, 84, 88, 92, 96, 00, and 04 (11 days) 

    /* test #2: Born Jan 22, 1998 and about to turn 2 */ 
    testIt(2, 1998, 1, 22, 2, 2 * 365); // no leap days 

    /* test #3: Born March 22, 1998 and about to turn 2 */ 
    testIt(3, 1998, 3, 22, 2, 2 * 365 + 1); // feb 29, 2000 

    /* test #4: Born March 22, 1898 and about to turn 2 */ 
    testIt(4, 1898, 3, 22, 2, 2 * 365); // no leap days 

    /* test #5: Born Jan 22, 1996 and about to turn 4 */ 
    testIt(5, 1996, 1, 22, 4, 4 * 365 + 1); // feb 29, 1996 

    /* test #6: Born March 22, 1996 and about to turn 4 */ 
    testIt(6, 1996, 3, 22, 4, 4 * 365 + 1); // feb 29, 2000 

    /* test #7: Born March 22, 1796 and about to turn 4 */ 
    testIt(7, 1796, 3, 22, 4, 365 * 4); // no leap days 

    /* test #8: Born Jan 1, 1800 and about to turn 101 */ 
    testIt(8, 1800, 1, 1, 101, 101 * 365 + 24); // There are 26 years divisible by 4 
           // between 1800 and 1901, all of these except 1800 and 1900 are leap years 
           // so there are 24 leap days 
    /* test #9: born Jan 1, 1900 and about to turn 100 */ 
    testIt(9, 1900, 1, 1, 100, 100 * 365 + 24); // 26 years divisible by 4 
           // 1900 was not a leap year, and the 100th brithday comes 
           // before Feb 29, 2000, so only 24 leap days. 


    if (num_tests_passed == 9) { 
     printf("all tests passed successfully. Well done!\n"); 
    } else { 
     printf("you only passed %d of the 9 tests. Looks like you've got some work to do\n", 
       num_tests_passed); 
    } 
} 

然後在project2.c你會做這個

/* ... */ 
int birth_year; 
int birth_month; 
int birth_day; 
int what_birthday; 
int num_days; 

/* functions... */ 

的名稱和您的解決方案

  • project2.h
  • myproject1的組成。 c
  • project2.c

然後該項目應該編譯時沒有任何鏈接錯誤。

1

也許你忽略從鏈接其他文件。

+0

不會外接int birth_year;自動鏈接? – user133466 2009-12-22 00:39:40

+0

編譯器是否神奇地知道它應該在哪個文件中找到實際的聲明? – 2009-12-22 00:43:28

+0

C不像Java,其中javac可以通過類的名稱告訴它所在的文件。您必須爲鏈接器提供所有要鏈接的.o文件的文件名。 'extern'只是說這個符號會在鏈接時解決,它沒有說明如何。 – 2009-12-22 00:46:06

0
  • 聲明在頭文件中的變量用EXTERN標籤
  • 聲明爲在.c文件
  • 包含在.c文件中頭文件的全局變量
相關問題