今天遇到一个头文件包含的问题,大家帮我看看:
globalVariables.h
#ifndef GLOBALVARIABLES_H
#define GLOBALVARIABLES_H
#include<QSemaphore>
#include<iostream>
extern int DataSize;
extern int BufferSize;
extern char buffer[];
extern QSemaphore freeSpace;
extern QSemaphore usedSpace;
#endif // GLOBALVARIABLES_H
globalVariables.cpp
#include<QSemaphore>
enum{DataSize=10000,BufferSize=4096};
char buffer[BufferSize]={0};
QSemaphore freeSpace(1024);
QSemaphore usedSpace(0);
下面是使用的类:
#ifndef CONSUMER_H
#define CONSUMER_H
#include <QThread>
#include "globalVariables.h"
class Consumer : public QThread
{
Q_OBJECT
public:
explicit Consumer(QObject *parent = 0);
void run();
};
#endif // CONSUMER_H
#include "consumer.h"
Consumer::Consumer(QObject *parent) :
QThread(parent)
{
}
void Consumer::run()
{
for(int i=0;i<DataSize;++i)
{
usedSpace.acquire(1);
std::cerr<<buffer[i%BufferSize];
freeSpace.release(1);
}
std::cerr<<std::endl;
}
然后编译的时候报错:
consumer.o: In function `Consumer::run()':
consumer.cpp:(.text+0x36): undefined reference to `DataSize'
make: Leaving directory `/home/robert/QtSDK/practice/chapter1/untitled'
consumer.cpp:(.text+0x68): undefined reference to `BufferSize'
consumer.cpp:(.text+0xa5): undefined reference to `DataSize'
报错原因说未定义的DataSize引用...应该是没初始化或者根本就没有,说明consumer.h的头文件里面并没有DataSize, BufferSize的定义.
大致看下代码,你的globalVariables.cpp并没有把globalVariables.h引入进来啊,DataSize,BufferSize等初始化都没做这个肯定报错的啊.你改下再试试看.