PDA

View Full Version : Problems compiling when using an extern



aarelovich
23rd June 2011, 21:16
Hi:

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


And my logoutput.h is like


#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


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 to `ERRORLOGGER'
/home/aarelovich/Dropbox/MineSim/main.cpp:9: undefined reference to `ERRORLOGGER'
/home/aarelovich/Dropbox/MineSim/main.cpp:13: undefined reference to `ERRORLOGGER'
/home/aarelovich/Dropbox/MineSim/main.cpp:15: undefined reference to `ERRORLOGGER'
obj/main.o: In function `main':
/home/aarelovich/Dropbox/MineSim/main.cpp:40: undefined reference to `ERRORLOGGER'
obj/mineedit.o:/home/aarelovich/Dropbox/MineSim/mineedit.cpp:101: more undefined references to `ERRORLOGGER' 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

mvuori
23rd June 2011, 21:29
Extern means that the variable is defined in a cpp file, which it is not yet. You need to add the line QString ERRORLOGGER; in your cpp file, preferably at the top of it, after the includes.

aarelovich
24th June 2011, 12:32
I did do that (in my main.cpp file) and I also removed logout.h. After that I only need to declare it as extern every time I need to use it in any part of the program that it is not in main.cpp. Thank you very much