我有一个 .h 文件,我打算仅将其用于存储将在我的程序中显示的所有信息字符串。在我的 info.h 中:
#ifndef __INFO_H
#define __INFO_H
char *info_msg = "This is version 1.0 of NMS.";
//all other strings used by view.c and controller.c
#endif
Run Code Online (Sandbox Code Playgroud)
然后在我的 view.h 我有如下:
//view.h
#ifndef __VIEW_H
#define __VIEW_H
#include "info.h"
//other stuff like method declaration etc.
#endif
Run Code Online (Sandbox Code Playgroud)
我的 controller.h 正在使用 view.h:
//controller.h
#ifndef __CONTROLLER_H
#define __CONTROLLER_H
#include "view.h"
#include "model.h"
//other stuff line method declaration etc.
#endif
Run Code Online (Sandbox Code Playgroud)
主文件:
#include "controller.h"
int main()
{
//stuff
}
Run Code Online (Sandbox Code Playgroud)
视图.c:
#include "view.h"
char esc,up,down,right,left;
void change_character_setting(char pesc, char pup, char pdown, char pright, char pleft)
{
esc = pesc;
up = pup;
down = pdown;
right = pright;
left = pleft;
}
void print_warning()
{
printf("%s \n",info_msg);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试创建可执行文件时,链接器会抱怨:
/tmp/ccqylylw.o:(.data+0x0): multiple definition of `info_msg'
/tmp/cc6lIYhS.o:(.data+0x0): first defined here
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会看到两个定义,因为我使用的是保护块。我试图在这里谷歌,但没有具体显示。有人可以解释它是如何看到多个定义的吗?我如何在 Java 中实现一些简单的事情,以便在 C 中使用单个文件进行所有文本操作?
您正在编译一个调用info_msg到每个源文件中的全局变量,该变量info.h直接包含或从其他某个头文件中提取。在链接时,链接器会找到所有这些info_msg标识符(在编译的每个目标文件中都有一个),但不知道使用哪一个。
将您的标题更改为:
#ifndef PROJ_INFO_H
#define PROJ_INFO_H
extern const char *info_msg; // defined in info.cpp
#endif
Run Code Online (Sandbox Code Playgroud)
并假设您有一个info.cpp(如果没有,您可以将它放在任何 .cpp 文件中,但那将是维护它的最自然的位置):
// info.cpp
#include "info.h"
const char *info_msg = "This is version 1.0 of NMS.";
Run Code Online (Sandbox Code Playgroud)
注意:在声明预处理器符号和标识符时要注意下划线的位置。根据C99标准:
C99 §7.1.3/1
- 所有以下划线和大写字母或另一个下划线开头的标识符始终保留供任何使用。
- 所有以下划线开头的标识符始终保留用作普通名称空间和标记名称空间中具有文件范围的标识符。