PDA

View Full Version : QT Creator, cross platform program.



kazek3018
15th December 2008, 19:52
Hello.

I have a small problem with QT Creator. Program will be run on Windows and Linux platform. I don't have problems on Linux but I have some on Windows. All the platform dependent parts of program I would put in #define test:

#ifdef unix
#include "*****.h"
#endif
#ifdef win32
#warning "Macro defined win32"
#include "******.h"
#else
#warning "Macro not defined win32"
#endif

I found in doc.

For projects that need to be built differently on each target platform, with many subdirectories, you can run qmake with each of the following options to set the corresponding platform-specific variable in each project file:
-unix
qmake will run in unix mode. In this mode, Unix file naming and path conventions will be used, additionally testing for unix (as a scope) will succeed. This is the default mode on all Unices.
-macx
qmake will run in Mac OS X mode. In this mode, Unix file naming and path conventions will be used, additionally testing for macx (as a scope) will succeed. This is the default mode on Mac OS X.
-win32
qmake will run in win32 mode. In this mode, Windows file naming and path conventions will be used, additionally testing for win32 (as a scope) will succeed. This is the default mode on Windows.

I don't have any idea why is not working on Windows.

jpn
15th December 2008, 19:57
You've mistaken macros with qmake scopes. See Q_OS_* and Q_WS_* definitions in QtGlobal header.

kazek3018
15th December 2008, 20:24
Thanks.

Now I got another problem, program is compiling and I get error from linker.

undefined reference to `TWinClass::TWinClass()'

Class structure is look like:


class TBaseClass
{};

TLinuxClass: public TBaseClass
{};

TWinClass: public TBaseClass
{};


in main.cpp i have this:

#ifdef Q_WS_X11
#include "tlinuxclass.h"
#endif
#ifdef Q_WS_WIN
#include "twinclass.h"
#endif

.....
TBaseClass *Base;
#ifdef Q_WS_X11
Base=new TLinuxClass;
#endif
#ifdef Q_WS_WIN
Base=new TWinClass;
#endif


Any idea?

Edit --------------------------------------------------
On Linux I got the same error from linker. If I change #ifdef Q_WS_X11 to #ifdef unix, then every thing is ok.
Edit2 -------------------------------------------------------
The same situation is with Q_OS_LINUX.

jpn
16th December 2008, 08:16
Sounds like TWinClass constructor is declared but not implemented. How about declaring the whole class once and just providing different implementation for it in different files:


// myclass.h:
class MyClass
{
public:
MyClass();
};



// myclass_unix.cpp:
#include "myclass.h"

MyClass::MyClass()
{
...
}



// myclass_win.cpp:
#include "myclass.h"

MyClass::MyClass()
{
...
}

In that case your .pro file would look like this:


HEADERS += myclass.h
unix:SOURCES += myclass_unix.cpp
win32:SOURCEs += myclass_win.cpp

kazek3018
16th December 2008, 10:12
All the class has it own file.

My .pro file is made by QT Creator. All Linux platform depended code is in

//#ifdef Q_OS_LINUX
//#ifdef Q_WS_X11
#ifdef unix

and now, everything is ok when I test for unix, but when I testing for Q_OS_LINUX or Q_WS_X11 then I got error from linker.

jpn
16th December 2008, 10:30
"unix", "win32" etc. scopes provided by qmake are used in qmake project files, not in source files. In source files you are supposed to use those Q_* macro definitions. And if you follow my example, you don't need to test macros at all because only appropriate files are compiled for each platform.

kazek3018
16th December 2008, 16:28
Thanks, now I understand the qmake scopes.

I don't really want to resign from Q_* macros and do it in your way because it will generate to many job for me. Now every thing work ok.

KaptainKarl
19th December 2008, 13:38
I tried QT Creator for awhile yesterday. I still like QDevelop (qdevelop.org) better; but that may be because I'm used to it.

Karl

wysota
20th December 2008, 23:13
I don't really want to resign from Q_* macros and do it in your way because it will generate to many job for me.

It is very unlikely you are correct with this statement. If you follow what jpn said, you will make a modification in one place only instead of inserting defines everywhere you need to do something not cross-platform. It is much much easier to maintain code if you have everything platform-dependent in one place.