我是C和NDK中的新成员,在我的应用程序中,我想要在不同文件之间保存和共享一些数据。所以我制作了包含静态字段的.c文件。每个类在C代码中创建自己的静态字段(NDK)
当我在一个文件中使用它时,它工作的很好,但是当我尝试在其他文件中使用它时,它没有我以前写在那里的数据。它看起来每一个文件创建自己内部我的静态字段的新实例
编辑
新增.h文件中。
代码test.h:
#ifndef IJKPLAYER_TEST_H
#define IJKPLAYER_TEST_H
void saveStartLoadingData();
int64_t getStartLoading();
void addToLoadingByte(int64_t bytesCount);
void endOfLoading();
void calculateAndSaveCurrentBitrate();
int64_t getDiff();
int64_t getLoadedBites();
int64_t getEndLoading();
int64_t getCurrentBitrate();
#endif //IJKPLAYER_TEST_H
test.c的:
#include "test.h"
#include <time.h>
static const int64_t ONE_SECOND= 1000000000LL;
extern int64_t start_loading;
extern int64_t end_loading ;
extern int64_t loaded_bytes;
extern int64_t currentBitrate;
extern int64_t diff;
int64_t now_ms() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (int64_t) now.tv_sec*1000000000LL + now.tv_nsec;
}
void saveStartLoadingData(){
loaded_bytes = 0LL;
start_loading = now_ms();
}
int64_t getStartLoading(){
return start_loading;
}
void addToLoadingByte(int64_t bytesCount){
loaded_bytes += bytesCount;
}
void endOfLoading(){
end_loading = now_ms();
diff = end_loading - start_loading;
}
void calculateAndSaveCurrentBitrate(){
currentBitrate = loaded_bytes * ONE_SECOND/ diff;
loaded_bytes = 0;
}
int64_t getDiff(){
return diff;
}
int64_t getLoadedBites(){
return loaded_bytes;
}
int64_t getEndLoading(){
return end_loading;
}
int64_t getCurrentBitrate(){
return currentBitrate;
}
现在我这包括以这种方式与其他文件:
#include "test.h"
EDIT 2
新错误
libavformat/avio.c:430: error: undefined reference to 'getStartLoading'
libavformat/avio.c:431: error: undefined reference to 'getEndLoading'
libavformat/avio.c:432: error: undefined reference to 'getLoadedBites'
C没有类。这不是OOPL。 – Olaf
@Olaf对不起,我的意思是文件 – Dima
像'int64_t start_loading;'这样的变量现在在你的test.c文件中丢失了。再次添加它们。我说你必须删除'static'。不是整条线。请重读我说的话。 – greenapps