C 程序无法识别头文件中的常量

标签 c gcc header-files

我有以下非常简单的头文件:

#ifndef __ZYNQ_CSORT_H__
#define __ZYNQ_CSORT_H__
#define CONSTANT    5
#endif

我将这个头文件包含在同一文件夹中的另一个 C 文件中。预处理器根本不提示头文件包含,但是当我尝试打印常量的值时,它告诉我它没有定义。有人知道怎么回事吗?

最佳答案

当我不确定预处理器在做什么时,我发现运行 C 预处理器本身常常会暴露问题。例如,给定 test1.h:

#ifndef TEST1_H
#define TEST1_H
/* In TEST1_H */
#define CONSTANT 5
#endif

...和test1.c:

#include "test1.h"
#include "test1.h"

int main(int argc, char **argv) {
    return CONSTANT;
}

... 运行 cpp -C test1.c test1.c.out(-C 参数使预处理器保留注释)给出 test1.c .out如下:

# 1 "test1.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test1.c"
# 1 "test1.h" 1


/* In TEST1_H */
# 2 "test1.c" 2


int main(int argc, char **argv) {
 return 5;
}

因此,对于我的情况,我可以确信包含了正确的头文件。

关于C 程序无法识别头文件中的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16137678/

相关文章:

python - pyenv 配置 : error: C compiler cannot create executables

c - 如何修复 "unable to open stdio.h in Turbo C"错误?

c - 使用i2c和ssh时需要tcp吗?

c++ - 如何让一个进程知道同一程序的其他进程

c - 将一个结构中的信息存储到另一个结构中

c - 使用 C 为日历控制台应用程序添加选项卡

c++ - 可变参数模板和函数的指针 : what compiler is right?

找不到gcc编译器

c - 错误 : expected '=' , ',' , ';' , 'asm' 或 '__attribute__' 在 C 中的 '*' token 之前

c++ - 你有没有写过没有守卫的标题?