Results 1 to 3 of 3

Thread: Problems compiling when using an extern

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problems compiling when using an extern

    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

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mineedit.h"
    3. #include "logoutput.h"
    4. #include <iostream>
    5.  
    6. void messageHandling(QtMsgType type, const char *msg){
    7.  
    8. if (ERRORLOGGER.isEmpty()){
    9. ERRORLOGGER = DEFERRORLOGGER;
    10. }
    11.  
    12. std::cout << "In Message Handling" << std::endl;
    13. std::cout << "Writing to file" << ERRORLOGGER.toStdString() << std::endl;
    14.  
    15. QFile file(ERRORLOGGER);
    16. file.open(QFile::Append);
    17. QTextStream stream(&file);
    18. switch (type) {
    19. case QtDebugMsg:
    20. stream << msg << "\n";
    21. file.close();
    22. break;
    23. case QtWarningMsg:
    24. stream << "WARNING: " << msg << "\n";
    25. file.close();
    26. break;
    27. case QtCriticalMsg:
    28. stream << "CRITICAL: " << msg << "\n";
    29. file.close();
    30. break;
    31. case QtFatalMsg:
    32. stream << "FATAL: " << msg << "\n";
    33. file.close();
    34. abort();
    35. }
    36. }
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. ERRORLOGGER = DEFERRORLOGGER;
    41. qInstallMsgHandler(messageHandling);
    42. QApplication a(argc, argv);
    43. MineEdit w;
    44. w.show();
    45. return a.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 

    And my logoutput.h is like
    Qt Code:
    1. #ifndef LOGOUTPUT_H
    2. #define LOGOUTPUT_H
    3.  
    4. #include <QString>
    5.  
    6. //----------------------------For outputting an error file--------------------------------
    7. #define DEFERRORLOGGER "/home/aarelovich/Documents/log.err"
    8. #define FOLDER_OUTPUT_LOG "./home/aarelovich/Documents"
    9. extern QString ERRORLOGGER;
    10.  
    11. #endif // LOGOUTPUT_H
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Problems compiling when using an extern

    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.

  3. The following user says thank you to mvuori for this useful post:

    aarelovich (24th June 2011)

  4. #3
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems compiling when using an extern

    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

Similar Threads

  1. Cross Compiling Problems
    By memoody in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 5th November 2010, 09:33
  2. Problems with Qt Mobility compiling!
    By cueMan in forum Installation and Deployment
    Replies: 1
    Last Post: 28th September 2010, 09:48
  3. problems with compiling in qt4.6
    By nataly in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2009, 12:09
  4. Compiling and efficiency problems
    By aarelovich in forum Qt Programming
    Replies: 12
    Last Post: 2nd October 2009, 16:38
  5. Problems compiling QMAKE
    By JNT in forum Installation and Deployment
    Replies: 4
    Last Post: 9th January 2007, 17:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.