我有一个漫长的过程,它会产生大约700 Mb的txt日志输出文件。这很难管理。所以我想将输出分成多个较小的日志文件。这就是我的main.cpp的样子
#include <QtGui/QApplication>
#include "mineedit.h"
#include "logoutput.h"
#include <iostream>
void messageHandling(QtMsgType type, const char *msg){
if (ERRORLOGGER.isEmpty()){
ERRORLOGGER = DEFERRORLOGGER;
}
std::cout << "In Message Handling" << std::endl;
std::cout << "Writing to file" << ERRORLOGGER.toStdString() << std::endl;
QFile file(ERRORLOGGER);
file.open(QFile::Append);
QTextStream stream(&file);
switch (type) {
case QtDebugMsg:
stream << msg << "\n";
file.close();
break;
case QtWarningMsg:
stream << "WARNING: " << msg << "\n";
file.close();
break;
case QtCriticalMsg:
stream << "CRITICAL: " << msg << "\n";
file.close();
break;
case QtFatalMsg:
stream << "FATAL: " << msg << "\n";
file.close();
abort();
}
}
int main(int argc, char *argv[])
{
ERRORLOGGER = DEFERRORLOGGER;
qInstallMsgHandler(messageHandling);
QApplication a(argc, argv);
MineEdit w;
w.show();
return a.exec();
}
[/CODE]
而我的logoutput.h就像
#ifndef LOGOUTPUT_H
#define LOGOUTPUT_H
#include <QString>
//----------------------------For outputting an error file------------------------------
#define DEFERRORLOGGER "/home/aarelovich/Documents/log.err"
#define FOLDER_OUTPUT_LOG "./home/aarelovich/Documents"
extern QString ERRORLOGGER;
#endif // LOGOUTPUT_H
现在在我的代码的一部分中,我做了:
ERRORLOGGER = name_of_current_log_file。
但是我得到以下编译错误:
obj/main.o:在函数
messageHandling(QtMsgType, char const*)':
/home/aarelovich/Dropbox/MineSim/main.cpp:8: undefined reference to
ERRORLOGGER'中/home/aarelovich/Dropbox/MineSim/main.cpp:9:对
ERRORLOGGER'
/home/aarelovich/Dropbox/MineSim/main.cpp:13: undefined reference to
ERRORLOGGER的 undefined reference /home/aarelovich/Dropbox/MineSim/main.cpp:15:对
ERRORLOGGER'
obj/main.o: In function
main的 undefined reference :/home/aarelovich/Dropbox/MineSim/main.cpp:40:遵循
ERRORLOGGER'
obj/mineedit.o:/home/aarelovich/Dropbox/MineSim/mineedit.cpp:101: more undefined references to
ERRORLOGGER的 undefined reference collect2:ld返回1退出状态
谁能告诉我我在做什么错?或者如何动态更改在其中创建应用程序日志的输出文件?
谢谢你的帮助
最佳答案
您的问题可能与extern变量有关。
Here是如何在c++中使用extern关键字的示例。
注意链接时,C++和C与extern关键字存在差异。
Basicall您需要做的是
global.cpp:
// declaration of g_nValue
int g_nValue = 5;
main.cpp:
// extern tells the compiler this variable is declared elsewhere
extern int g_nValue;
int main()
{
g_nValue = 7;
return 0;
}
在您的示例中,如果您在 logoutput.h 中使用
extern QString ERRORLOGGER;
,就像在链接中说明的那样,需要在另一个cpp中声明此变量。
我希望这有帮助
关于qt - Qt使用外部变量进行编译的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6460209/