c - 在一个文件中定义变量,但在另一个文件中未定义(在公共头文件中声明)
问题描述
我有一些功能在单独的文件功能中突出显示。c。此文件中的代码读取位于functionality.h 中的阈值:
unsigned int thresold1[2];
unsigned int thresold2[2];
void *watcher(void *);
我也有 main.c 文件,我试图在其中配置这些阈值:
#include "functionality.h"
int main(void) {
/* ... */
int ret;
pthread_t watcher_thread;
thresold1[0] = 10;
thresold1[1] = 50;
ret = pthread_create(&watcher_thread, NULL, watcher, NULL);
if (ret) {
/* ... */
}
/* ... */
}
但是当我试图watcher()
从function.c 中访问这些阈值时,所有这些数组值都归零,即未定义。我哪里错了?
PS 功能.h 也包含在功能.c 中
UPD:我是这样编译的:
gcc -pthread main.c functionality.c -o main
解决方案
变量应在头文件中声明为:
extern unsigned int thresold1[2];
extern unsigned int thresold2[2];
并在一个独特的点(.c
文件)中定义为:
unsigned int thresold1[2];
unsigned int thresold2[2];
推荐阅读
- macos - Fsharp.Data, HtmlDocument 没有扩展名 Descendants
- java - 如何根据配置动态创建多个jms消息驱动的通道适配器
- javascript - JS地图/过滤器在self上设置属性
- html - 在不移动其余元素的情况下向元素添加填充
- python - 如何匹配具有两个不同值的单个字段并在mongoDB聚合python中计算匹配的文档?
- canvas - 手绘卸妆工具
- html - 根据 json 数据的值对 html 数据中的行进行分组。并在点击每个显示详细信息的值时展开行
- aspnetboilerplate - 在 ABP Angular 中创建新租户时已使用角色名称 Admin
- r - R 轴图上的实际数据
- amazon-s3 - 如何在不使用签名 URL 的情况下安全地提供 S3 文件?