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

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

问题描述

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

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.

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

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);

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

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

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

...那么编译器适时报错:assign of read-only location 'bar'.

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

const 放在名称之前的语法似乎并不像应用于函数参数那样应用于变量,下面的两行似乎实际上是等效的:

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;

... 因为将 y 定义为 int y; 会导致错误:y"的类型限定符冲突,正如我所期望的那样,如果 x 被定义为 int x;.

... 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;.

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

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).

我在这里阅读了许多相关问题,但我发现没有一个问题可以为 C(相对于 C++ 或 C#)提供明确的答案:

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 — 通过 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.

模块.h

int module_get_x (void);

模块.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天全站免登陆