Thursday, August 7, 2008

.local domain name tweaks

Lately i suffered from a incompatible DNS zone name condition in my openSuSE 11.
I set up a small network, with "lab.local" internal DNS zone. nslookup,dig,host and other DNS utilities resolved names in it fine, but ping,gethostip and other gethostname()-derived apps didnot.
The reason was the feature of the current resolver algorithm in linux.
in order to fix it I had to add "mdns off" line into /etc/host.conf.
"man host.conf" !

Thursday, July 10, 2008

C++ common errors

Collection of my favorite C++ errors.

1) incorrect typecast:
double p = 0.3333;
int N = 50000, m=30000
int dn = (int)p*M-m;
2) signed - unsigned integer improper use:
unsigned int X=45;
double x = -Х*0.5;
x == ???
3) template misuse:
class Base {}
class Derived : public Base {}

std::vector<Base> v;
Derived d;
v.push_back(d);
and the last one, my favorite:
double p = 2/3; Happy coding!

Wednesday, January 30, 2008

Visual C++ 2005 project using QT and qmake

Recently, one may find lots of rumours about future acvisition of Trolltech by Nokia.
I would'nt worry much about it. QT is GPL, and that means, we will always have it around.

Recently, i started some QT development, and previously i was doing it in my favorite OpenSuSE environment, using KDevelop 3.5. Untill I started to do some really hardcore programming, utilizilng lost of BOOST and Computational Geometry (CGAL). I found out, that Visual C++ 2005 has more comfortable Debugger, so i decided to switch my development back to Windows XP (thanx to MSDN AA) and VC++ 2005.

As you might already know, the "official"OpenSource version of QT/Windows supports only MinGW GCC, which sucks. Previously, one had to use some magick patches (here is an instruction: how to patch QT to work with Visual C++ 2005) . Last time i tried to use them, i had non-working binaries in "Release" configuration. But the salvation came unexpectedly from KDE project, with their release of KDE for Windows.

The KDE for Windows itself is not much usefull -- there is no Desktop Environment yet, only some Applications available. Its biggest value is ... the Libraries!!!! and, of cource.. QT! wich supports VC2005 natively, witouht further patching. Here is small 1-2-3 how to start developing QT OpenSource apps in windows:

1) use Kde Windows Installer to install QT, and other libs that you might need (Boost? image handling libs? eigen? libz ? whatever...)
2) Get VC++ 2005 Express Edition (Kde installer may also help)

say, you have installed everything to D:\KDE, then i recommend to save it as Windows Environment variable (say, QTHOME) -- may be handy for team development, and stuff.
Now you are ready to create a "Hello world" app.

1) Open VisualStudio, create a new "Makefile" project. (Figure 1)


2) it will prompt for "build" "rebuild" and "clean" commands for Debug and Release configurations. For these i create 6 different custom .bat files, and specify them on prompt.



Your solution dir now has "helloworld" subdir. There we'll have to create several files:

main.cpp: (add it to the VS project as "source file")
#include
#include
#include

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton hello("Hello world!");
hello.resize(100, 30);
hello.show();
return app.exec();
}


helloworld.pro
-- the QMake project file
SOURCES += main.cpp
TEMPLATE = app
CONFIG += warn_on thread qt stl
TARGET = helloworld
FORMS +=
HEADERS +=
INCLUDEPATH +=


build_debug.bat
qmake
nmake Debug

rebuild_debug.bat
clean_debug.bat build_debug.bat
clean_debug.bat
qmake nmake clean

now copy the last three into build_release.bat, rebuild_release.bat and clean_release.bat accoordingly, replacing "Debug" into "Release". you may add them into the VC project, for example, into another project subfolder, for later convininent modifications.



now you may press the "Build Solution" button, and see what happens.
the last two things remain:
1) Debugger. You need to specify the executable.


2) Intellisense. You need to specify the includepaths for QT and other libs.


Now, you may check at the QPushButton hello object how it works.


The last step: i used kde-installer 0.8.5 which installed QT 4.3.3. For some reason, the makefiles, created with QMAKE did not create the moc_- files.
i had lots of errors like:
moc_mainwindow.cpp
c1xx : fatal error C1083: Cannot open source file: 'debug\moc_mainwindow.cpp': N
o such file or directory
moc_mainview.cpp
c1xx : fatal error C1083: Cannot open source file: 'debug\moc_mainview.cpp': No
such file or directory



To fix this, i had to manually rewrite my qt.conf, which resides in the bin directory of KDE installation.
for example, if you have installed KDE into D:\KDE, then you'd have to create/edit the file
D:\KDE\bin\qt.conf:
[Paths]
Prefix=D:\\KDE\\
Documentation=D:\\KDE\\doc
Headers=D:\\KDE\\include
Libraries=D:\\KDE\\lib
Binaries=D:\\KDE\\bin
Plugins=D:\\KDE\\plugins
Data=D:\\KDE\\
Translations=D:\\KDE\\translations
Settings=D:\\KDE\\etc
Examples=D:\\KDE\\examples
Demos=D:\\KDE\\demos





that's all, folks! Happy Hacking!