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

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

问题描述

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



有一个标准的可接受的方式来声明整数(在库头文件中),允许应用程序读取变量中的值,但是告诉编译器生成一个错误,如果任何尝试生成写回它的代码?
例如,如果我要声明一个函数

  extern void foo (int const bar); 

...然后呼叫者被允许传递一个局部变量:

  int bar = 0; 
foo(bar);

...但如果函数声明尝试写入 bar

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

...然后编译器会报告错误:只读位置'bar'。



在名称之前放置 const 的语法似乎不适用于与函数参数相同的方式,下面的两行似乎有效地相当:

  extern const int x; 
extern int const y;

...因为将y定义为 int y; 导致错误:'y'的冲突类型限定词,我希望看到已经被定义为 int x;



我知道我可以通过声明和定义返回值(只能用作r值)的访问器函数来解决问题。



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



C - 通过const声明访问非const / a>



混合extern和const



请有人指出我如何实现它的一个例子的方向,或者确认我怀疑它不是语法上的可以实现?

解决方案


在库中有一个变量应该是只读的应用程序链接库,但可以从定义它的编译单元中修改。


解决方案是使用面向对象的设计,也就是所谓的私有封装



module.h

  int module_get_x(void); 

module.c

  static int x; 

int module_get_x(void)
{
return x;
}

main.c

  #includemodule.h

int main(void)
{
int value = 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天全站免登陆