我怎样才能在C模块中定义两个全局的`const`变量为相同的值? [英] How can I define two global `const` variables to the same value in a C module?

查看:160
本文介绍了我怎样才能在C模块中定义两个全局的`const`变量为相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 .c 文件中使用了一对全局变量,匹配到一个 extern 声明每个在两个不同的 .h 文件(以及一个 .h 文件,预处理两种方式)。一个用于公共消费,一个用于私人用途。它们都是 const 变量。

I’m using a pair of global variables in one of my .c files, matched to a single extern declaration each in two different .h files (well, one .h file, preprocessed two ways). One is for public consumption, and one is for private use. Both are const variables.

我只想初始化我的 .c 文件,然后将第二个变量分配给相同的内容。以下是目前 .c 文件的相关内容:

I only want to initialize one of the variables in my .c file, and then assign the second variable to the same content. Here’s the relevant contents of the .c file at the moment:

struct List_methods const List = {
  .create         = List__create,
  .create_naughty = List__create_naughty,
  // …
};
struct List_methods const Paws__List = List;

...和相应的.h:

… and the corresponding .h:

#if defined(EXTERNALIZE)
# define  List_methods  Paws__List_methods
# define  List          Paws__List
#endif

// …

struct List_methods {
  list  (*create)         (void);
  list  (*create_naughty) (void);
  // …
} const extern List;

#if defined(EXTERNALIZE)
# undef   List_methods  Paws__List_methods
# undef   List          Paws__List
#endif

这里的目标是确保当 EXTERNALIZE包含 .h 定义,包含文件可以访问 Paws__List 变量, extern 'd定义在我的 .c 文件中。但是,如果包含而没有那个定义,它们可以访问 extern 'd List 而是(我打算在我的内部文件中使用它,并且如果 #include er需要它)。

The goal here, is to ensure that when the .h is included with EXTERNALIZE defined, the including file gains access to the Paws__List variable, extern’d to the definition in my .c file. However, if it’s included without that definition, they gain access to an extern’d List instead (which I intend to use in my internal files, and make available if the #includeer wants it).

然而,在我的编译器中发生了 Paws__List = List 赋值,出现以下错误:

However, the Paws__List = List assignment blows up in my compiler, with the following error:

Source/Paws.o/list/list.c:32:40: error: initializer element is not a
      compile-time constant
struct List_methods const Paws__List = List;
                                       ^~~~

我正在寻找任何可以帮助我做出的帮助如上所述(即,为我的 .c 文件中的同一结构定义两个 const 名称,例如那一个或另一个可以被 .h 头引用。)

I’m looking for any help I can get to make this work as described above (that is, to define two const names for the same struct in my .c file, such that one or the other can be referenced by the .h header.)

推荐答案

如果我正确地理解了你,你想定义一个接口(vtable),并且实现将根据EXTERNALIZE的不同而不同:

If i understand you correctly, you want to define an interface (vtable) and the implementation will vary depending on EXTERNALIZE:

我会走那条路:


/* header file */
/* define the interface */
typedef struct List_methods {
  list  (*create)         (void);
  list  (*create_naughty) (void);
  // …
} List_Methods;

const extern List_methods Paws_List; // note: double underscores are afaik not allowed
const extern List_methods Other_List; // take any name but List

#ifdef EXTERNALIZE
# define  List          Paws_List
#else
# define  List          Other_List
#end

重要的是这些结构是相同的类型,否则您不能将其分配给
other。其次,我不会用别名覆盖符号。这只会在您想要在.c文件中同时使用这两个问题时出现问题。

The important thing is that the structures are of the same type else you cannot assign one to the other. Second I would't override a symbol with an alias. This only makes problems when you want to use both for example in your .c file.

这篇关于我怎样才能在C模块中定义两个全局的`const`变量为相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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