开发者

Qt Problems compiling using an extern variable

I have a rellly long process that produces about 700 Mb of a txt log output file. This is very hard to manage. So I want to divide the output in multiple smaller log files. This is what my main.cpp looks like

#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]

And my logoutput.h is like

#ifndef LOGOUTPUT_H
#define LOGOUTPUT_H

#include <QString>

//----------------------------For outputting an err开发者_StackOverflow中文版or file------------------------------
#define         DEFERRORLOGGER             "/home/aarelovich/Documents/log.err"
#define         FOLDER_OUTPUT_LOG          "./home/aarelovich/Documents"
extern QString  ERRORLOGGER;

 #endif // LOGOUTPUT_H

Now in a part of my code I do: ERRORLOGGER = name_of_current_log_file.

However I get the following compilation errors: obj/main.o: In function messageHandling(QtMsgType, char const*)': /home/aarelovich/Dropbox/MineSim/main.cpp:8: undefined reference toERRORLOGGER' /home/aarelovich/Dropbox/MineSim/main.cpp:9: undefined reference to ERRORLOGGER' /home/aarelovich/Dropbox/MineSim/main.cpp:13: undefined reference toERRORLOGGER' /home/aarelovich/Dropbox/MineSim/main.cpp:15: undefined reference to ERRORLOGGER' obj/main.o: In functionmain': /home/aarelovich/Dropbox/MineSim/main.cpp:40: undefined reference to ERRORLOGGER' obj/mineedit.o:/home/aarelovich/Dropbox/MineSim/mineedit.cpp:101: more undefined references toERRORLOGGER' follow collect2: ld returned 1 exit status

Can anyone please tell me what am I doing wrong? Or how I can dynamically change the output file in which I create my application log?

Thanks for any help


Your problem is probably related to extern variable.

Here is an example of how to use extern keyword in c++.

Beware that C++ and C have differences with extern keyword when linking.

Basicall what you need to do is

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;
    }

In your example if you use extern QString ERRORLOGGER; in logoutput.h, this variable needs to be declared in another cpp just as explained in the link.

I hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜