Results 1 to 5 of 5

Thread: [solved] Qt 4.7.0 MinGW undefined reference to extern symbols

  1. #1
    Join Date
    May 2006
    Location
    Stuttgart, Germany
    Posts
    22
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [solved] Qt 4.7.0 MinGW undefined reference to extern symbols

    Hi,

    just installed Qt 4.7.0 MinGW (gcc version 4.4.0) and tried to compile a simple console example with QTextStream stdin/stdouts. I got undefined references when linking with MinGW/ld linker, as for Visual C++ 2008 all went fine.

    Qt Code:
    1. #ifndef QSTD_H
    2. #define QSTD_H
    3.  
    4. #include <QTextStream>
    5. #include <QFile>
    6. #include <QString>
    7.  
    8. /** @short helper objects and functions which help reduce the
    9.   need for char[] and the standard library.
    10.  
    11.   defines three @ref QTextStream instances
    12.   which behave like the c++ standard iostreams, bound to the
    13.   standard in/out/error.
    14.  
    15.   Also provided, some helper functions for writing
    16.   interactive stdin/stdout applications.
    17. */
    18. //start
    19. namespace qstd {
    20.  
    21. /** @short An alias for standard input
    22.   */
    23. extern QTextStream cin; /* declared only, defined in the .cpp file */
    24. /** @short An alias for standard output
    25.   */
    26. extern QTextStream cout;
    27. /** @short An alias for standard error
    28.   */
    29. extern QTextStream cerr;
    30. /** yes/no prompt
    31.   interactive stdin UI - prompts user with
    32.   a yes/no question. Repeatedly-asks
    33.   until user supplies a valid answer.
    34.  
    35.   @param yesNoQuestion the yes/no question
    36.   @return true/false depending on what the
    37.   user responded.
    38.   */
    39. bool yes(QString yesNoQuestion);
    40. /** Convenience function that feeds a specific question
    41.   to the yes() function.
    42.   @usage do {.....} while(more ("foobar"));
    43.   so that user sees the question: "Another foobar (y/n)? "
    44.   @param name of the item being handled by the loop.
    45.   */
    46. bool more(QString prompt);
    47. /** A function for safely taking an int from the keyboard.
    48.   Takes data into a QString and tests to make sure it
    49.   can be converted to int before returning.
    50.   @param base allows choice of number base.
    51.   @return returns validated int.
    52.   */
    53. int promptInt(int base = 10);
    54. /** A function for safely taking a double from the keyboard.
    55.   Takes data into a QString and tests to make sure it
    56.   can be converted to double before returning.
    57.   @return returns validated int.
    58.   */
    59. double promptDouble();
    60. /** Complete dialog for opening a file for output.
    61.   Asks user for file name, checks to see if
    62.   file already exists and, if so, asks the user if
    63.   it is ok to overwrite.
    64.   @param Reference QFile parameter is set to point
    65.   to the (eventually) opened file.
    66.   */
    67. /** @short Dialog for a output file prompt
    68.   */
    69. void promptOutputFile(QFile& outfile);
    70.  
    71. /** @short Dialog for input file prompt */
    72. void promptInputFile(QFile& infile);
    73.  
    74.  
    75. //end
    76. }
    77.  
    78. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //start id=namespace
    2. #include "qstd.h"
    3.  
    4. /* QTextStreams look a lot like iostreams,
    5. we just have to point them to the right place. */
    6.  
    7. //start id=streamdefs
    8. QTextStream qstd::cin(stdin, QIODevice::ReadOnly);
    9. QTextStream qstd::cout(stdout, QIODevice::WriteOnly);
    10. QTextStream qstd::cerr(stderr, QIODevice::WriteOnly);
    11. //end
    12.  
    13. [...]
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtDebug>
    3. #include "qstd.h"
    4.  
    5.  
    6. using namespace qstd;
    7.  
    8. int main(int argc, char* argv[])
    9. {
    10. QCoreApplication app(argc, argv);
    11.  
    12. QString str1("Test");
    13.  
    14. cout << str1 << endl;
    15.  
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    When compiling with Qt 4.7.0/MinGW I get linker error:
    Qt Code:
    1. mingw32-make[1]: Entering directory `D:/projekt/work/test/qstring_test'
    2. g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-re
    3. oc -Wl,-subsystem,console -mthreads -Wl -o debug\qstring_test.exe debug/qstd.o
    4. ebug/qstring_test.o -L"d:\Qt\2010.05\qt\lib" -lQtCored4
    5. debug/qstring_test.o: In function `main':
    6. D:\projekt\work\test\qstring_test/qstring_test.cpp:14: undefined reference to `
    7. qstd::cout'
    8. collect2: ld returned 1 exit status
    9. mingw32-make[1]: *** [debug\qstring_test.exe] Error 1
    10. mingw32-make[1]: Leaving directory `D:/projekt/work/test/qstring_test'
    11. mingw32-make: *** [debug] Error 2
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? Thanks for any hints
    Last edited by AlGaN; 5th October 2010 at 12:52. Reason: partially solved

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7.0 MinGW undefined reference to extern symbols

    what happens when u use qstd::cout ...? the errors shows that "cout" is treated as std::cout by mingw.

  3. #3
    Join Date
    May 2006
    Location
    Stuttgart, Germany
    Posts
    22
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.7.0 MinGW undefined reference to extern symbols

    Hi,

    sorry, that was a cut&paste error, the error states qstd:cout of course...
    Compiling with Visual C++ 2008 does not show any error, so it must be something with MinGW/gcc I suppose

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7.0 MinGW undefined reference to extern symbols

    all seems to be correct... try to put all of the code in qstd.cpp file inside the qstd namespace and remove qstd:: from the defination.

  5. The following user says thank you to nish for this useful post:

    AlGaN (5th October 2010)

  6. #5
    Join Date
    May 2006
    Location
    Stuttgart, Germany
    Posts
    22
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.7.0 MinGW undefined reference to extern symbols

    Hi MrDeath

    thanks! That works! Now I wonder why there is a difference in MinGW in defining symbols in a namespace and full qualifiying them in the signature?
    Anyway, thanks for your help!

Similar Threads

  1. undefined reference
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2009, 13:45
  2. Undefined reference to crt
    By derektaprell in forum Installation and Deployment
    Replies: 0
    Last Post: 20th October 2009, 08:34
  3. Undefined Reference To...
    By ManuMies in forum Qt Programming
    Replies: 6
    Last Post: 10th February 2009, 12:14
  4. /usr/bin/ld: Undefined symbols:
    By ataffard in forum Qt Programming
    Replies: 0
    Last Post: 2nd May 2008, 01:24
  5. Undefined symbols in Qtopia
    By minguzzi in forum Installation and Deployment
    Replies: 0
    Last Post: 13th September 2006, 13:59

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.