简体   繁体   English

创建全局常量C字符串的正确方法

[英]Proper way to create global constant C strings

I'm using C and this is what I'm doing to instantiate a global C string 我正在使用C,这是我要实例化全局C字符串的操作

In the header of a single file 在单个文件的标题中

const char SINGLE_MSG[] = "single msg";

In the headers of all other files 在所有其他文件的标题中

extern const char SINGLE_MSG[];

It always works fine when compiling in C mode, but seems to create binder errors when compiling in C++ mode. 在C模式下进行编译时,它总是可以正常工作,但在C ++模式下进行编译时,似乎会产生绑定程序错误。 Here is an example of such errors: 这是此类错误的示例:

pksw_nd_proc_test2.dev32.i0.pr.obj : error LNK2001: unresolved external symbol "char const * const SINGLE_MSG" (?SINGLE_MSG@@3QBDB)

I've seen a similar question where they also specify the buffer length. 我看到了一个类似的问题 ,他们还指定缓冲区长度。 I'd rather not, since if don't want to have to remember changing that if I decide to change the string value in the source. 我宁愿不这样做,因为如果我不想更改,就决定更改源中的字符串值。 That's the whole point to make this global, so every instance sees the same value. 这就是使它成为全局变量的重点,因此每个实例都具有相同的值。

Am I doing something wrong? 难道我做错了什么?

Since it's a constant, would it be better to make it static as well? 由于它是一个常量,因此最好也使其静态化吗? It would be fine for me to have extra copies of it, as long as I don't have to copy the string value manually in the source to do the initialization. 只要我不必在源中手动复制字符串值来进行初始化,对我来说,拥有额外的副本就可以了。

Edit: please note that I'm using a program that I don't fully control. 编辑:请注意,我使用的是我无法完全控制的程序。 It's a network simulator that lets me define a "header block" for each node. 这是一个网络模拟器,可让我为每个节点定义一个“标题块”。 I do not have any way to explicitly include the header of a node in the header of another node. 我没有任何方法可以在另一个节点的头中明确包含一个节点的头。

That's why I was using extern in C, and the different way globals work between the 2 languages is probably the reason I get the linker errors. 这就是为什么我在C中使用extern的原因,而这两种语言之间的全局变量工作方式不同可能是导致我收到链接器错误的原因。

According to the answers, the solution is to make a new, separate header file containing the definitions of the global variables and include that in all other headers. 根据答案,解决方案是制作一个新的单独的头文件,其中包含全局变量的定义,并将其包括在所有其他头文件中。

When you create a global constant variable in the header, then that variable never actually gets committed to memory. 在标头中创建全局常量变量时​​,该变量实际上永远不会提交到内存。

What happens instead is that any code that uses that variable simply replaces any reference with "single msg". 相反,发生的事情是,使用该变量的所有代码都将所有引用替换为“单个msg”。

This means there is no need use extern, but in turn does mean you must always include that header if you use the variable. 这意味着不需要使用extern,但是反过来,这意味着如果您使用变量,则必须始终包含该标头。

What extern is actually used for is exposing a variable that has been declared in a .cpp file, and therefore is in memory somewhere, to other cpp files. extern实际用于将在.cpp文件中声明的变量(因此存在于内存中的某个地方)暴露给其他cpp文件。 This is only necessary if you want a global but dynamic variable. 仅在需要全局但动态的变量时才需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM