我想用以下方式组织我的c ++变量和函数:头文件"stuff.h"中的函数原型,"stuff.cpp"中的函数实现,然后在main.cpp中说#include"stuff.h"(所以我可以调用stuff.cpp中实现的函数.到现在为止还挺好.现在我想在stuff.cpp中声明一些具有全局作用域的变量(所以我可以修改stuff.cpp 和 main.cpp中实现的函数中的变量).这似乎不起作用.我怎样才能做到这一点?
将它们声明为extern.例如,在stuff.h中:
extern int g_number;
Run Code Online (Sandbox Code Playgroud)
然后在stuff.cc中:
int g_number = 123;
Run Code Online (Sandbox Code Playgroud)
然后在main.cc中#include stuff.h.