是否c。提供一种方式来声明一个外部变量为“只读”,但将其定义为可写的? [英] Does C provide a way to declare an extern variable as 'read-only', but define it as writeable?

查看:124
本文介绍了是否c。提供一种方式来声明一个外部变量为“只读”,但将其定义为可写的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发为使用GCC C.在库中的嵌入式产品一个硬件抽象库有应只读链接库中的应用程序中的变量,但可以从编译单元内进行修改是定义它。

有没有一个标准,申报整数(库头文件)可以接受的方式,将允许应用程序读取变量中的值,而是告诉编译器生成一个错误,如果任何试图生成code,它写回呢?
例如,如果我要声明的功能的是:

 的extern无效美孚(INT常量吧);

...那么调用者被允许通过一个局部变量:

  INT吧= 0;
富(巴);

...但如果​​函数的声明试图写

 无效美孚(INT常量条)
{
    巴= 99;
}

...那么编译器duely报告错误:只读的位置栏分配

放置常量的语法之前的名称没有出现要应用于变量相同的方式,它的功能的参数,和下面的两行似乎可有效等价的:

 的extern const int的X;
INT的externÿ常量;

...因为定义y为 INTŸ;在误差结果:冲突的类型预选赛'Y',我会希望看到拥有X被定义为 INT X;

我知道我可以通过声明并定义一个返回值(这只能作为R值)的存取功能得到解决此问题。

我读了一些在这里的相关问题,但没有我发现提供了对C一个明确的答案(而不是C ++或C#):

<一个href=\"http://stackoverflow.com/questions/8051969/c-accessing-a-non-const-through-const-declaration\">C - 通过常量声明访问非const

混合extern和const的

请能在我一个人点我怎么可能实现呢,还是证实了我的怀疑,它在语法上不实现的一个例子的方向?


解决方案

  

内库有应只读链接库中的应用程序中的变量,但可以从编译单元定义它内进行修改。


该解决方案是使用面向对象的设计,即所谓的私人封装

module.h中

  INT module_get_x(无效);

的module.c

 静态INT X;INT module_get_x(无效)
{
  返回X;
}

的main.c

 的#includemodule.h中INT主要(无效)
{
  int值= module_get_x();
}

如果该变量必须在模块内部读写,那么这就是的只有的适当做到这一点的方式。所有其他的方法都只是意大利面条编程的一些味道。

I'm developing a hardware abstraction library for an embedded product using GCC C. Within the library there is a variable that should be read-only to the application that links the library, but can be modified from within the compilation unit that defines it.

Is there a standard, acceptable way to declare the integer (in the library header file) that will allow the application to read the value in the variable, but tell the compiler to generate an error if any attempt is made to generate code that writes back to it? For example, if I were to declare a function as:

extern void foo(int const bar);

... then the caller is permitted to pass a local variable:

int bar = 0;
foo(bar);

... but if the function declaration attempts to write to bar:

void foo(int const bar)
{
    bar = 99;
}

... then the compiler duely reports an error: assignment of read-only location 'bar'.

The syntax of placing the const before the name does not appear to apply to variables in the same way that it does to function parameters, and the two lines below seem to be effectively equivalent:

extern const int x;
extern int const y;

... because defining y as int y; results in an error: conflicting type qualifiers for 'y', as I would expect to have seen had x been defined as int x;.

I know that I can get around the problem by declaring and defining an accessor function that returns the value (which can only be used as an r-value).

I've read a number of related questions here, but none that I've found provide a definitive answer for C (as opposed to C++ or C#):

C — Accessing a non-const through const declaration

Mixing extern and const

Please can someone point in me the direction of an example of how I might achieve it, or confirm my suspicion that it is not syntactically achievable?

解决方案

Within the library there is a variable that should be read-only to the application that links the library, but can be modified from within the compilation unit that defines it.

The solution is to use object-oriented design, namely something called private encapsulation.

module.h

int module_get_x (void);

module.c

static int x;

int module_get_x (void)
{
  return x;
}

main.c

#include "module.h"

int main(void)
{
  int value = module_get_x();
}

If the variable must be read-write inside the module, then this is the only proper way to do this. All other ways are just some flavour of spaghetti programming.

这篇关于是否c。提供一种方式来声明一个外部变量为“只读”,但将其定义为可写的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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