管理文件之间的全局变量 [英] Managing global vars between files

查看:63
本文介绍了管理文件之间的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原因:我正在开发一个嵌入式项目,内存非常有限
内存(512字节以下,其中60左右是堆栈空间),

翻译进入有限的堆栈空间。为了节省堆栈空间,我试图只使用参数和堆栈空间来实现真正的b $ b b临时性的东西。我将数据结构声明为

全局变量,并且所有函数都直接访问它,而不是将指针传递给应该总是用数据填充的数据结构。这很好用

,直到我尝试将代码组织成单独的文件。


问题:我知道我可以声明一个全局var in 1文件,并声明它在另一个导出中导致
因此我可以在文件中使用相同的全局变量,

但这导致除主要源文件之外的每个文件都有一个很好的

开头的导出变量的长列表。有没有更好的方法来获取C中的全局变量,除了在1个文件中声明,并且每隔一个使用变量的文件导出


Reason: I am working on an embedded project which has very limited
memory (under 512 bytes, 60 or so of which is stack space), which
translates into limited stack space. In order to save on stack space, I
tried to only use parameters and stack space for things which are truely
temporary. Instead of passing a pointer to a data structure which should
always be populated with data, I have the data structure declared as a
global variable and all functions access it directly. This works fine
until I try and organize the code into seperate files.

Problem: I know I can declare a global var in 1 file, and declare it
with export in another so I can use that same global var across files,
but this leads to every file, except the main source file, having a nice
long list of exported vars at the beginning. Is there a better way to
make global vars in C, outside of declaring in 1 file, and exporting in
every other file that uses the variable?

推荐答案

Kleenex< kl ***** @ no.nospam>写在

新闻:RNj4d.106446
Kleenex <kl*****@no.nospam> wrote in
news:RNj4d.106446


yh.72915@fed1read05:
yh.72915@fed1read05:
原因:我正在努力嵌入式项目,内存非常有限(512字节以下,其中60个左右是堆栈空间),这意味着有限的堆栈空间。为了节省堆栈空间,我试图只使用参数和堆栈空间来实现真正的临时性。我没有将指针传递给应该总是用数据填充的数据结构,而是将数据结构声明为全局变量,并且所有函数都直接访问它。这很好用
直到我尝试将代码组织成单独的文件。


我已经使用了没有全局变量的较小部件(128B),但这不是重点

,我承认。

问题:我知道我可以在1个文件中声明一个全局变量,并在另一个文件中声明它导出,所以我可以在文件中使用相同的全局变量,
但是这会导致每个文件,除了主要的源文件,在开头有很长的导出变量列表。是否有更好的方法在C语言中创建全局变量,除了在1个文件中声明,并导出
每个使用变量的其他文件?
Reason: I am working on an embedded project which has very limited
memory (under 512 bytes, 60 or so of which is stack space), which
translates into limited stack space. In order to save on stack space, I
tried to only use parameters and stack space for things which are truely
temporary. Instead of passing a pointer to a data structure which should
always be populated with data, I have the data structure declared as a
global variable and all functions access it directly. This works fine
until I try and organize the code into seperate files.
I''ve used smaller parts (128B) without globals but that''s not the point
here, I admit.
Problem: I know I can declare a global var in 1 file, and declare it
with export in another so I can use that same global var across files,
but this leads to every file, except the main source file, having a nice
long list of exported vars at the beginning. Is there a better way to
make global vars in C, outside of declaring in 1 file, and exporting in
every other file that uses the variable?




如果你有一个数据库然后将全局''数据全部放在一个结构中。

然后将结构声明和struct对象的extern放在一个

文件中,比如db.h ,并将其包含在需要

数据库的所有文件中。


-

- Mark - >

-



If you have a "database" of global''ish data then put them all in a struct.
Then put the struct declaration and an extern of the struct object in a
file, like db.h, and include it from all files that require the
"database".

--
- Mark ->
--


Kleenex< kl ***** @ no.nospam>写道:
Kleenex <kl*****@no.nospam> writes:
问题:我知道我可以在1个文件中声明一个全局变量,并在另一个文件中声明它导出,因此我可以在文件中使用相同的全局变量,
但是这导致除了主源文件之外的每个文件在开头都有一个很长的导出变量列表。有没有更好的方法在C语言中创建全局变量,除了在1个文件中声明,并且
导出使用变量的每个其他文件?
Problem: I know I can declare a global var in 1 file, and declare it
with export in another so I can use that same global var across files,
but this leads to every file, except the main source file, having a
nice long list of exported vars at the beginning. Is there a better
way to make global vars in C, outside of declaring in 1 file, and
exporting in every other file that uses the variable?




C没有任何名为export的东西。它有extern,

虽然。


使用全局变量的常用方法是声明它们一次,

在头文件中使用extern,例如


foo.h:


extern int foo;

extern double bar [];

extern void(* x)(double,int,...);


然后每个文件使用globals包含标题:


#include" foo.h"


正好一个.c文件应该实际定义全局变量,

可选择初始值设定项:


int foo = 20;

double bar [] = {1,2,3,42, 67,128};

void(* x)(double,int,...);

-

欢迎光临这是一个未定义行为的奇妙世界,其中恶魔

是鼻子,而且死亡站用户很紧张。 --Daniel Fox



C doesn''t have anything called "export". It has "extern",
though.

The usual way to use global variables is to declare them once,
with "extern", in a header file, e.g.

foo.h:

extern int foo;
extern double bar[];
extern void (*x) (double, int, ...);

Then each file that uses the globals includes the header:

#include "foo.h"

Exactly one .c file should actually define the global variables,
optionally with initializers:

int foo = 20;
double bar[] = {1, 2, 3, 42, 67, 128};
void (*x) (double, int, ...);
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox


这篇关于管理文件之间的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆