C 文件无法看到 #include 头文件中定义的变量

标签 c global-variables header-files

a.h中我有

#ifndef A_H
#define A_H 1

const int global_val = 10;

#endif

a.c中我有

#include "a.h"

int someFunc() {
    return global_val;
}

但是编译器 (gcc) 在编译 a.c 时会抛出错误:'global_int' 未声明。当我包含头文件时,它怎么可能是未声明的?

最佳答案

您应该有两个文件。

啊啊

  #ifndef A_H
  #define A_H 1

  extern const int global_val;

  #endif

空调

  #include "A.h"


  const int global_val = 10;

关于C 文件无法看到 #include 头文件中定义的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066780/

相关文章:

c - String Token strtok函数逻辑

c - 在ubuntu linux中查找用户的资源利用率

swift - swift中从外部函数调用值变量

c - 如何列出包含头文件的所有文件

c - 预期标识符或 '('

c - C 如何在有符号整数和无符号整数中存储负数?

c++ - 从用户读取整数并写入子管道,然后从中读取父管道

c - 在 yacc 中声明全局变量

javascript - 设置全局基本 URL

c - 在 C 程序和 shell 脚本之间共享头文件