我想使用 MPLAB X IDE(用于 PIC 开发的 ide)创建 C 语言库。
我的库 ABCLib 有一个像这样的简单源文件:
文件 abc.c
void abcTest(int n){
// I want store n as global variable
}
要在 MyProject 中使用此库,我必须在 MyProject header 文件夹中创建 abc.h:
文件 abc.h
#ifndef _ABC_H
#define _ABC_H
void abcTest(int n);
#endif;
文件MyProject.c(主文件)
#include "abc.h"
void main(void) {
abcTest(10);
}
现在,我想将 n 存储为全局变量,因此,在调用 abcTest() 后,我可以在任意位置检索 n 的值。
我正在开发一个库,打算在我的所有项目中重用它。
最佳答案
您需要在想要使用其全局值的所有文件中将该变量声明为外部变量。
示例:
文件 abc.c
extern int global_a;
void abcTest(int n){
global_a+=n;
}
文件 abc.h
#ifndef _ABC_H
#define _ABC_H
void abcTest(int n);
#endif;
文件MyProject.c(主文件)
#include "abc.h"
int global_a = 0; // always initialize global variables
void main(void) {
abcTest(10);
}
问候
关于c - 如何使用 MPLAB X IDE 在库项目中创建全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10540731/