Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 错误-头文件中静态常量int的重新定义_C_Makefile_Header Files - Fatal编程技术网

C 错误-头文件中静态常量int的重新定义

C 错误-头文件中静态常量int的重新定义,c,makefile,header-files,C,Makefile,Header Files,我的头文件中有以下行: static const int SET_LENGTH = 16; 这是我尝试创建一个常量变量,它可以在包含头文件的多个.c文件中使用。 我使用makefile编译头文件和两个C文件,如图所示: myset:myset.c set.c set.h gcc -ansi -pedantic -Wall myset.c set.c -o myset 我将标题包括在c文件中,如图所示: 第c组: myset.c: #include "set.h" #

我的头文件中有以下行:

static const int SET_LENGTH = 16;
这是我尝试创建一个常量变量,它可以在包含头文件的多个.c文件中使用。 我使用makefile编译头文件和两个C文件,如图所示:

myset:myset.c set.c set.h
   gcc -ansi -pedantic -Wall myset.c set.c -o myset
我将标题包括在c文件中,如图所示: 第c组:

myset.c:

#include "set.h"
#include "set.c"
当我尝试编译时,出现以下错误:

error: redefinition of 'SET_LENGTH' static const int SET_LENGTH=16;
此外,本说明:

note: previous definition of 'SET_LENGTH' was here static const int SET_LEGNTH = 16;

有人能帮忙吗?

#包括“set.c”
?包含一个C文件几乎从来都不是正确的做法。删除它。我想在myset.c文件中访问它的函数。还有其他方法吗?您不需要将实现包含在同一个文件中。所需要的只是函数声明。您已经通过
#包括“set.h”
。编译器(实际上是链接器)将在您使用所显示的命令构建实现时找到它。@ItaiElidan无法修改此变量,因此它只会充当定义可能会浪费内存、性能更差的变量。这是你想要的吗?@Itaelidan作为一个非常初学者,听更有经验的程序员说。您可以使用#define。这里有一个工作示例:
note: previous definition of 'SET_LENGTH' was here static const int SET_LEGNTH = 16;