qt - Qt使用外部变量进行编译的问题

标签 qt compiler-errors extern

我有一个漫长的过程,它会产生大约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/

相关文章:

c++ - Qt QDomElement 性能问题

python - Shiboken无法导入

android - 找不到PhoneGap引用错误

c++ - 创建全局 C++ 对象

c++ - 按下 X 时 QDialog 没有立即关闭,如何让它不在顶部?

c++ - 更改 QHeaderView 空间缓冲区

java - 在 token ","上出现语法错误,{预期在此 token 之后

Java 编译错误 : Parameter x is exceeding the limit of 255 words

c++ - 通过未知大小的外部普通数组进行循环

c - 在 'header.h' 文件中声明了一个自引用结构并试图在 'main.c' 中定义它,这会导致错误