Friday, April 25, 2014

Qt in Windows Vista, MinGW and QJson (qt-google-maps)


I am having quite a rough week. For a project I am using the Google Maps API within C++-code. As I prefer to use a Linux environment but usually my clients use Windows, I have become a big fan of the Qt framework.


After some research, I found an C++ client created by Integrated Computer Solutions, Inc. (ICS) that uses the Google Maps API: qt-google-maps . I was not yet familiar with the usage of Google APIs or JSON and this seemed like a good start. I quickly noticed that their code didn't survive the transition from Google Maps API version 2 to version 3 and changed their code, making it work.

Currently, everything runs fine on my Linux box. The problem comes when I tried to install QJson on Windows (my current laptop is still from the Vista-era, but it is supposed to work):

1. following the QJson build instructions


I am truely respecting the great work Flavio Castelli has (and is) putting in this project. I had no trouble getting QJson to work under Linux. The problem now came with trying to build the code in Windows. The build instructions are easy to understand and Flavio even proves it to work in one of his blog posts:

The CLI way

After having installed both Qt and cmake add the following line at the end of qtenv.bat (it can be found under C:\Qt\qt_version\bin):

set PATH=%PATH%;C:\path_to_cmake_\bin

Open the “Qt Command Prompt”, go to the directory containing QJson’s sources and do:

mkdir build
cd build
cmake -G "MinGW Makefiles" ..
mingw32-make
mingw32-make install


Now you will find libqjson.dll under the build\lib directory.

The GUI way

The Qt Creator IDE can handle cmake-based projects.

In addition to the Qt Windows SDK you have to download and install the Qt Creator IDE.

Then follow these steps:
  1. Start Qt Creator.
  2. Select: File → Open file or project.
  3. Go to the directory containing QJson’s sources.
  4. Open the CMakeLists.txt file.
  5. Open the Projects tab and ensure the build directory points to the path where qjson source code is located.
  6. Select: Build → Build all.
At the end of the build you will find the .dll file under the lib folder.
Unfortunately, when I try either of these I get an error saying that MinGW's g++ compiler did not pass a simple compiling test[note to self: must find exact words from error]

2. MinGWversion garbled...


So no problem with QJson. It seemed that the MinGW-version I installed with Qt is not complete.

Little Qt history: back in the day, I needed two downloads to get Qt running: the Qt SDK (well, actually that is enough to get the compiling done) and additionaly the Qt Creator to get a graphical working environment. I don't know where or when it changed, is it because of the several switches of groups overseeing the project (a little history: first developed by Trolltech, acquired by Nokia, now run by Digia for commercial users and the "Qt Project" for the opensource version) or simply the version switch from Qt4 to Qt5?

It made the updating of my Qt platform a bit more complicated. (looking back, it is quite simple, but I got stuck in the old habits and I guess something went wrong with overwriting my Qt4 with Qt5, garbling my installed MinGW version).

I kept looking for the Qt SDK, but the links always refer to http://qt.nokia.com/downloads , which simply doesn't work anymore (see the littly history lesson above) and the archive does not contain the SDK anymore, apparently?

3. What needs to be done nowadays?


I solved this one by downloading the Qt 4.8.6-version: Qt libraries 4.8.6 for Windows (MinGW 4.8.2, 328 MB) (Info) from qt-project.org/downloads .
If you have no MinGW-version installed yet, the solution is easy, the installer will provide you the link with the proper MinGW-version to download!
In my case, I had a garbled MinGW-version on my computer, and the installer did complain that is the wrong version, but I did not find the solution. The solution I followed was to download a fresh MinGW version: i686-4.8.2-release-posix-dwarf-rt_v3-rev3.7z .

Unpack the archive and overwrite your previous MinGW version (or put it in a different directory and make Qt use that version, also make sure the PATH-variable is set correctly).

4. Install Cmake


Rather straightforward: download the Windows installer and run it, the default options are perfectly fine.

5. back to the QJson build instructions


Actually the simplest way is to use the command line:
The CLI way

After having installed both Qt and cmake add the following line at the end of qtenv.bat (it can be found under C:\Qt\qt_version\bin):

set PATH=%PATH%;C:\path_to_cmake_\bin
With that difference that Qt4 does not use qtenv.bat but qtvars.bat . Simply add the instruction, in my case: set PATH=%PATH%;C:\Wim\CMake2.8\bin .
Open the “Qt Command Prompt”, go to the directory containing QJson’s sources and do:

mkdir build
cd build
cmake -G "MinGW Makefiles" ..
With me, it now finally starts to go okay :-) Result:
C:\Wim\qjson-master\build> cmake -G "MinGW Makefiles" ..
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: C:/Wim/mingw32/bin/gcc.exe
-- Check for working C compiler: C:/Wim/mingw32/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Wim/mingw32/bin/g++.exe
-- Check for working CXX compiler: C:/Wim/mingw32/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
Qt5 not found, searching for Qt4
-- Looking for Q_WS_X11
-- Looking for Q_WS_X11 - not found
-- Looking for Q_WS_WIN
-- Looking for Q_WS_WIN - found
-- Looking for Q_WS_QWS
-- Looking for Q_WS_QWS - not found
-- Looking for Q_WS_MAC
-- Looking for Q_WS_MAC - not found
-- Found Qt4: C:/Wim/Qt/4.8.6/bin/qmake.exe (found version "4.8.6")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Wim/qjson-master/build
Great!
mingw32-make
Result:

C:\Wim\qjson-master\build> mingw32-make
[  9%] Generating moc_serializerrunnable.cxx
[ 18%] Generating moc_parserrunnable.cxx
Scanning dependencies of target qjson
[ 27%] Building CXX object src/CMakeFiles/qjson.dir/
parser.cpp.obj
[ 36%] Building CXX object src/CMakeFiles/qjson.dir/qobjecthelper.cpp.obj
[ 45%] Building CXX object src/CMakeFiles/qjson.dir/json_scanner.cpp.obj
[ 54%] Building CXX object src/CMakeFiles/qjson.dir/json_parser.cc.obj
[ 63%] Building CXX object src/CMakeFiles/qjson.dir/parserrunnable.cpp.obj
[ 72%] Building CXX object src/CMakeFiles/qjson.dir/serializer.cpp.obj
[ 81%] Building CXX object src/CMakeFiles/qjson.dir/serializerrunnable.cpp.obj
[ 90%] Building CXX object src/CMakeFiles/qjson.dir/moc_parserrunnable.cxx.obj
[100%] Building CXX object src/CMakeFiles/qjson.dir/moc_serializerrunnable.cxx.o
bj
Linking CXX shared library libqjson.dll
[100%] Built target qjson

mingw32-make install
Here it went wrong and I got an error cannot write to C:\Program Files\qjson [note to self: must find exact words from error]restart the Qt Command Prompt and run it as Administrator. Go back to the directory containing QJson’s sources and enter the directory "build" before running the command "mingw32-make install":

C:\Wim\qjson-master\build>mingw32-make install
[100%] Built target qjson
Install the project...
-- Install configuration: ""
-- Installing: C:/Program Files/qjson/lib/cmake/qjson/
QJSONTargets.cmake
-- Installing: C:/Program Files/qjson/lib/cmake/qjson/QJSONTargets-noconfig.cmak
e
-- Installing: C:/Program Files/qjson/lib/cmake/qjson/QJSONConfig.cmake
-- Installing: C:/Program Files/qjson/lib/cmake/qjson/QJSONConfigVersion.cmake
-- Installing: C:/Program Files/qjson/lib/libqjson.dll.a
-- Installing: C:/Program Files/qjson/bin/libqjson.dll
-- Installing: C:/Program Files/qjson/include/qjson/parser.h
-- Installing: C:/Program Files/qjson/include/qjson/parserrunnable.h
-- Installing: C:/Program Files/qjson/include/qjson/qobjecthelper.h
-- Installing: C:/Program Files/qjson/include/qjson/serializer.h
-- Installing: C:/Program Files/qjson/include/qjson/serializerrunnable.h
-- Installing: C:/Program Files/qjson/include/qjson/qjson_export.h

Now you will find libqjson.dll under the build\lib directory.

Not entirely correct, or not for this version. I found the dll file (as the above result shows) in the directory C:/Program Files/qjson/bin/ .

Still, when I run my code, the compiler complains in can't resolve #include <QJson/parser> and the dll doesn't seem to have effect, the program crashes immediately after being called. Sad but true...
---
Work in progress, article will be reviewed and updated in due time.

4 comments:

  1. problem solved, hope to post solution soon

    ReplyDelete
  2. Could you update your post with the solution? I'm stuck at the same point as well.

    ReplyDelete
    Replies
    1. Currently I am a bit busy on another project. Can you send me an email so I can help you in that way? Could be faster than updating the blog.

      Is your problem related to QJson or MingW? What is the output that you get?

      Delete
  3. I am having the same problem. Mine is QJson related. Could I have the solution as well? samcataps@gmail.com

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...