【发布时间】:2014-10-03 02:32:19
【问题描述】:
我有这样的事情:
--includes.h
extern int count;
--main.cpp
#include "includes.h"
int count = 4;
--other.cpp
#include "includes.h"
cout<<count; // will output 4
但是当我这样做时,编译器会出错并显示以下消息:
error LNK2001: unresolved external symbol "int count" (?count@@3HA)
知道我为什么会得到这个吗?
在不同文件之间共享变量的最佳方式是什么? 如何在一个文件中定义使用变量,并在另一个文件中修改相同的变量?
【问题讨论】:
-
使用函数和标题。
-
您是否链接到相应的目标文件?可能不会。
-
两个 .cpp 文件在同一个项目中,对?
-
是的,他们在同一个项目中
-
请说明您是如何调用链接器的。
标签: c++ extern unresolved-external