PDA

View Full Version : [solved] Qt 4.7.0 MinGW undefined reference to extern symbols



AlGaN
5th October 2010, 10:41
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.



#ifndef QSTD_H
#define QSTD_H

#include <QTextStream>
#include <QFile>
#include <QString>

/** @short helper objects and functions which help reduce the
need for char[] and the standard library.

defines three @ref QTextStream instances
which behave like the c++ standard iostreams, bound to the
standard in/out/error.

Also provided, some helper functions for writing
interactive stdin/stdout applications.
*/
//start
namespace qstd {

/** @short An alias for standard input
*/
extern QTextStream cin; /* declared only, defined in the .cpp file */
/** @short An alias for standard output
*/
extern QTextStream cout;
/** @short An alias for standard error
*/
extern QTextStream cerr;
/** yes/no prompt
interactive stdin UI - prompts user with
a yes/no question. Repeatedly-asks
until user supplies a valid answer.

@param yesNoQuestion the yes/no question
@return true/false depending on what the
user responded.
*/
bool yes(QString yesNoQuestion);
/** Convenience function that feeds a specific question
to the yes() function.
@usage do {.....} while(more ("foobar"));
so that user sees the question: "Another foobar (y/n)? "
@param name of the item being handled by the loop.
*/
bool more(QString prompt);
/** A function for safely taking an int from the keyboard.
Takes data into a QString and tests to make sure it
can be converted to int before returning.
@param base allows choice of number base.
@return returns validated int.
*/
int promptInt(int base = 10);
/** A function for safely taking a double from the keyboard.
Takes data into a QString and tests to make sure it
can be converted to double before returning.
@return returns validated int.
*/
double promptDouble();
/** Complete dialog for opening a file for output.
Asks user for file name, checks to see if
file already exists and, if so, asks the user if
it is ok to overwrite.
@param Reference QFile parameter is set to point
to the (eventually) opened file.
*/
/** @short Dialog for a output file prompt
*/
void promptOutputFile(QFile& outfile);

/** @short Dialog for input file prompt */
void promptInputFile(QFile& infile);


//end
}

#endif




//start id=namespace
#include "qstd.h"

/* QTextStreams look a lot like iostreams,
we just have to point them to the right place. */

//start id=streamdefs
QTextStream qstd::cin(stdin, QIODevice::ReadOnly);
QTextStream qstd::cout(stdout, QIODevice::WriteOnly);
QTextStream qstd::cerr(stderr, QIODevice::WriteOnly);
//end

[...]




#include <QCoreApplication>
#include <QtDebug>
#include "qstd.h"


using namespace qstd;

int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);

QString str1("Test");

cout << str1 << endl;

return 0;
}


When compiling with Qt 4.7.0/MinGW I get linker error:


mingw32-make[1]: Entering directory `D:/projekt/work/test/qstring_test'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-re
oc -Wl,-subsystem,console -mthreads -Wl -o debug\qstring_test.exe debug/qstd.o
ebug/qstring_test.o -L"d:\Qt\2010.05\qt\lib" -lQtCored4
debug/qstring_test.o: In function `main':
D:\projekt\work\test\qstring_test/qstring_test.cpp:14: undefined reference to `
qstd::cout'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\qstring_test.exe] Error 1
mingw32-make[1]: Leaving directory `D:/projekt/work/test/qstring_test'
mingw32-make: *** [debug] Error 2


What am I doing wrong? Thanks for any hints

nish
5th October 2010, 11:01
what happens when u use qstd::cout ...? the errors shows that "cout" is treated as std::cout by mingw.

AlGaN
5th October 2010, 11:53
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

nish
5th October 2010, 12:03
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.

AlGaN
5th October 2010, 12:38
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! :)