吸气剂C中的塞特犬

时间:2016-08-26 14:23:16

标签: c memory setter getter

我在C中面临一个问题,我正在尝试使用一些getter和setter来共享多个源文件之间的变量。

我在这里使用getter和setter声明我的变量(ok_button):

variable.c

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include "../libhd_src/libhd.h"

int ok_button;

void set_ok_button(int value){
    ok_button=value;
    printf("Setting ok");
} 

int get_ok_button(){
    return ok_button;
}

在这里,当我按下一个按钮时,它将变量设置为1.(无法上传此源文件的完整代码,但我在日志中看到按下时我正确执行了函数set_ok_button(我看到了)每当我按下按钮时,printf“设置OK”))

button.c的

#include "../libhd_src/libhd.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>

 void * button_back_center_short(void *arg){
    set_ok_button(1);
    return 0;
  }

在这里,我只需使用getter函数检查变量的值。

read.c

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include "../../libhd_src/libhd.h"



int main(int argc, char **argv){

while(1){
      printf("Value %d", get_ok_button());
      usleep(500000);
        }
}   

问题是read.c中显示的值总是“0”,即使按下我的按钮并将值设置为1 ...

有人明白什么是错的吗?如果你看到更好的解决方案,请随时告诉我:)

1 个答案:

答案 0 :(得分:0)

我认为您的问题可能是您在不同的文件中有多个set_ok_buttonget_ok_button个功能。确保只在一个文件中定义它们,并在标题中添加2行声明(但不定义)函数:

void set_ok_button(int value);
int get_ok_button();