gpt4 book ai didi

全局变量的类型 C 冲突

转载 作者:行者123 更新时间:2023-11-30 18:48:42 31 4
gpt4 key购买 nike

我想使用一个文件来保存所有#define 和常量:示例

 #ifndef CONSTANTS_H_
#define CONSTANTS_H_

#include <avr/io.h>

//OVERALL DEFS
#define TRUE 1
#define FALSE 0


// CLK_32MHz.h
const uint8_t NEW_CLOCK_FREQ = 0b00000010;

#endif /* CONSTANTS_H_ */

然后用一个不同的文件来调用它。示例:

#ifndef CLK_32MHZ_H_
#define CLK_32MHZ_H_

#include <avr/io.h>
#include "constants.h"

extern uint8_t NEW_CLOCK_FREQ;

void Change_CLK_32HZ(){

//Set the clk config
OSC_CTRL = NEW_CLOCK_FREQ;

//Wait for the right flag to be set in the OSC_STATUS reg
while((OSC_STATUS & PIN1_bm) != PIN1_bm);

//Write the “IOREG” signature to the CPU_CCP reg
CPU_CCP = CCP_IOREG_gc;

//Select the new clock source in the CLK_CTRL reg
CLK_CTRL = CLK_SCLKSEL_RC32M_gc;

return;
}
#endif /* CLK_32MHZ_H_ */

这是我的主要功能:

#include <avr/io.h>
#include "constants.h"
#include "Clk_32MHz.h"


int main(void){

}

这是我在终端上收到的错误:

“NEW_CLOCK_FREQ”文件的类型限定符冲突:Clk_32MHz.h之前“NEW_CLOCK_FREQ”的定义在这里 文件:constants.h

最佳答案

尝试以下操作...

在 header.h 中:

extern const uint8_t myconst;

在 foo.c 中:

#include "header.h"
const uint8_t myconst = 42;

关于全局变量的类型 C 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982685/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com