我在编译时收到以下错误消息,但我确定我在外部 Project2.c 文件中声明了变量。有人可以给我一个提示我做错了什么吗?谢谢
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);
}
}
最佳答案
在您的 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 <stdio.h>
#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... */
你的解决方案包括
然后项目应该编译没有任何链接器错误。
关于c - 'extern' 使用错误——未解析的外部符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1943522/