Build and deploy c++ QGIS custom application on windows
After a lot of troubles, I managed to compile and deploy a QGIS c++ app on windows. This small guide will describe the steps I followed. This has been tested on win xp and windows 7, both in 32 bits.
Development environment
Your app must be built using MSVC 9.0 (2008) since QGIS in OSGeo’s package was built with it. Hence, MinGW cannot be used.
- Install Microsoft Visual Studio Express 2008.
- Install QGIS and Qt libs using OSGeo4W installer
- Install Qt Creator
- If you want a debugger,you should install CDB. This can be achieved by installing Windows SDK environment. In the installation process, only select Debugging toos for windows.
I wasn’t able to use the compiler yet, so I am not 100% sure about 4.
Now, if you want to build using Qt Creator, it must be started in a proper environment. Adapt this batch to launch Qt Creator:
ECHO Setting up QGIS DEV ENV set PYTHONPATH= set OSGEO4W_ROOT=C:\OSGeo4W call "%OSGEO4W_ROOT%\bin\o4w_env.bat" @set QMAKESPEC=win32-msvc2008 @set PATH=%OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis-dev\bin;%PATH% @set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\include;%OSGEO4W_ROOT%\apps\qgis-dev\include @set LIB=%LIB%;%OSGEO4W_ROOT%\lib;%OSGEO4W_ROOT%\apps\qgis-dev\lib path %OSGEO4W_ROOT%\bin;%SYSTEMROOT%\System32;%SYSTEMROOT%;%SYSTEMROOT%\System32\wbem;C:\Progra~1\Git\bin;C:\Qt\qtcreator-3.0.1\bin;%PATH% set VS90COMNTOOLS=C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\ call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86 start "Qt Creator" /B C:\Qt\qtcreator-3.0.1\bin\qtcreator.exe %*
Then, you need to configure a proper kit in Qt Creator.
- Go to Options -> Build & Run -> Compilers and check that Microsoft Visual C++ Compiler 9.0 is correctly detected.
- Then in Qt Versions tab, add Qt from the OSGeO installation, normally c:\OSGeo4W\bin\qmake.exe
- In Debuggers tab, add cdb.exe found in c:\Debugging tools for windows\
- Finally, check in Kits that it is properly configured.
Building the application
This is what looks like an application project file.
QT += core gui xml greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = hfp TEMPLATE = app SOURCES += YOURSOURCES HEADERS +=YOUR HEADERS FORMS += YOUR FORMS RESOURCES += images/images.qrc win32:CONFIG(Release, Debug|Release) { LIBS += -L"C:/OSGeo4W/lib/" -lQtCore4 LIBS += -L"C:/OSGeo4W/lib/" -lQtGui4 LIBS += -L"C:/OSGeo4W/lib/" -lQtXml4 LIBS += -L"C:/OSGeo4W/apps/qgis-dev/lib/" -lqgis_core LIBS += -L"C:/OSGeo4W/apps/qgis-dev/lib/" -lqgis_gui } else:win32:CONFIG(Debug, Debug|Release) { PRE_TARGETDEPS += C:/OSGeo4W/lib/QtCored4.lib PRE_TARGETDEPS += C:/OSGeo4W/lib/QtGuid4.lib PRE_TARGETDEPS += C:/OSGeo4W/lib/QtXmld4.lib LIBS += -L"C:/OSGeo4W/lib/" -lQtCored4 LIBS += -L"C:/OSGeo4W/lib/" -lQtGuid4 LIBS += -L"C:/OSGeo4W/lib/" -lQtXmld4 LIBS += -L"C:/OSGeo4W/apps/qgis-dev/lib/" -lqgis_core LIBS += -L"C:/OSGeo4W/apps/qgis-dev/lib/" -lqgis_gui } win32:{ INCLUDEPATH += C:/OSGeo4W/include DEPENDPATH += C:/OSGeo4W/include INCLUDEPATH += C:/OSGeo4W/apps/qgis-dev/include DEPENDPATH += C:/OSGeo4W/apps/qgis-dev/include DEFINES += GUI_EXPORT=__declspec(dllimport) CORE_EXPORT=__declspec(dllimport) } unix { LIBS += -L/usr/local/lib/ -lqgis_core -lqgis_gui LIBS += -L/usr/local/lib/qgis/plugins/ -lgdalprovider INCLUDEPATH += /usr/local/include/qgis DEFINES += GUI_EXPORT= CORE_EXPORT= }
Remarks
- GUI_EXPORT and CORE_EXPORT must be set to __declspec(dllimport). I don’t know exactly what it means, but I found out reading this thread, with some hazardous tries. If you don’t set these, you won’t be able to call any variable defined as extern in QGIS (e.g. cursors).
- Qt release libraries shall not be mixed up with debug config in your project. In other words, use release libs for release mode and debug libs for debug mode.
With this, you should be able to compile your QGIS application in Qt Creator!
You can find some coding examples on github which are a bit old but still useful to start.
Now, to get the whole potential of QGIS libs, you must initialize the QgsApplication in your main window class:
#if defined(Q_WS_WIN) QString pluginPath = "c:\\OSGeo4W\\apps\\qgis-dev\\plugins"; QString prefixPath = "c:\\OSGeo4W\\apps\\qgis-dev\\"; #else QString pluginPath = "/usr/local/lib/qgis/plugins/"; QString prefixPath = "/usr/local"; #endif QgsApplication::setPluginPath( pluginPath ); QgsApplication::setPrefixPath( prefixPath, true); QgsApplication::initQgis();
Deploying on windows
Since QGIS is not to be installed on the target computer, the built app will not be able to find the path declared in previous code.
There is probably a better approach, but here is a way to solve this:
Change the path to
QString pluginPath = "c:\\myapp\\qgis\plugins"; QString prefixPath = "c:\\myapp\\qgis";
This means you must deploy the app to this exact location: c:\myapp. In this directory, you need to create a qgis folder in which you will copy c:\OSGeo4W\apps\qgis-dev\resources and c:\OSGeo4W\apps\qgis-dev\plugins.
Besides, this you will need to copy some DLLs to be able to run the applications. You might want to use the dependency walker to find which are needed.
The batch file hereafter creates a folder on the building machine that will contain all the needed files in my case (it might be different in your case).
rmdir c:\myapp /Q /S mkdir c:\myapp mkdir c:\myapp\iconengines mkdir c:\myapp\qgis mkdir c:\myapp\qgis\resources mkdir c:\myapp\qgis\plugins copy PATHTOMYAPP\build-myapp-Desktop-Release\release\myapp.exe c:\myapp\ copy c:\OSGeo4W\bin\QtCore4.dll c:\myapp\ copy c:\OSGeo4W\bin\QtGui4.dll c:\myapp\ copy c:\OSGeo4W\bin\QtXml4.dll c:\myapp\ copy c:\OSGeo4W\bin\QtNetwork4.dll c:\myapp\ copy c:\OSGeo4W\bin\QtSvg4.dll c:\myapp\ copy c:\OSGeo4W\bin\QtWebKit4.dll c:\myapp\ copy c:\OSGeo4W\bin\zlib_osgeo.dll c:\myapp\ copy c:\OSGeo4W\bin\msvcr71.dll c:\myapp\ copy c:\OSGeo4W\bin\phonon4.dll c:\myapp\ copy c:\OSGeo4W\bin\proj.dll c:\myapp\ copy c:\OSGeo4W\bin\geos_c.dll c:\myapp\ copy c:\OSGeo4W\bin\gdal110.dll c:\myapp\ copy c:\OSGeo4W\bin\ogdi_32b1.dll c:\myapp\ copy c:\OSGeo4W\bin\libexpat.dll c:\myapp\ copy c:\OSGeo4W\bin\xerces-c_3_1.dll c:\myapp\ copy c:\OSGeo4W\bin\LIBPQ.dll c:\myapp\ copy c:\OSGeo4W\bin\SSLEAY32.dll c:\myapp\ copy c:\OSGeo4W\bin\LIBEAY32.dll c:\myapp\ copy c:\OSGeo4W\bin\krb5_32.dll c:\myapp\ copy c:\OSGeo4W\bin\comerr32.dll c:\myapp\ copy c:\OSGeo4W\bin\k5sprt32.dll c:\myapp\ copy c:\OSGeo4W\bin\gssapi32.dll c:\myapp\ copy c:\OSGeo4W\bin\hdf_fw.dll c:\myapp\ copy c:\OSGeo4W\bin\mfhdf_fw.dll c:\myapp\ copy c:\OSGeo4W\bin\jpeg_osgeo.dll c:\myapp\ copy c:\OSGeo4W\bin\jpeg12_osgeo.dll c:\myapp\ copy c:\OSGeo4W\bin\netcdf.dll c:\myapp\ copy c:\OSGeo4W\bin\geotiff.dll c:\myapp\ copy c:\OSGeo4W\bin\libtiff.dll c:\myapp\ copy c:\OSGeo4W\bin\sqlite3.dll c:\myapp\ copy c:\OSGeo4W\bin\spatialite4.dll c:\myapp\ copy c:\OSGeo4W\bin\freexl.dll c:\myapp\ copy c:\OSGeo4W\bin\iconv.dll c:\myapp\ copy c:\OSGeo4W\bin\libxml2.dll c:\myapp\ copy c:\OSGeo4W\bin\LIBMYSQL.dll c:\myapp\ copy c:\OSGeo4W\bin\hdf5.dll c:\myapp\ copy c:\OSGeo4W\bin\szip.dll c:\myapp\ copy c:\OSGeo4W\bin\libcurl.dll c:\myapp\ copy c:\OSGeo4W\bin\zlib1.dll c:\myapp\ copy c:\OSGeo4W\bin\openjp2.dll c:\myapp\ copy c:\OSGeo4W\bin\spatialindex1.dll c:\myapp\ copy c:\OSGeo4W\bin\qwt5.dll c:\myapp\ copy c:\OSGeo4W\apps\qt4\plugins\iconengines\qsvgicon4.dll c:\myapp\iconengines\ copy C:\Progra~1\Git\bin\libiconv-2.dll c:\myapp\ copy C:\Progra~1\Git\bin\libintl-8.dll c:\myapp\ copy c:\OSGeo4W\apps\qgis-dev\bin\qgis_Core.dll c:\myapp\ copy c:\OSGeo4W\apps\qgis-dev\bin\qgis_gui.dll c:\myapp\ copy c:\OSGeo4W\apps\qgis-dev\bin\msvcp90.dll c:\myapp\ copy c:\windows\system32\msvcp100.dll c:\myapp\ copy c:\windows\system32\msvcr100.dll c:\myapp\ copy C:\OSGeo4W\apps\qgis-dev\resources\* c:\myapp\qgis\resources copy C:\OSGeo4W\apps\qgis-dev\plugins\* c:\myapp\qgis\plugins
To be able to run the app, on a fresh windows XP, I had to install:
- Microsoft Visual C++ 2008 Redistributable Package (x86)
- Microsoft Visual C++ 2005 Redistributable Package (x86)
-
Microsoft Visual C++ 2005 Service Pack 1 Redistributable Package MFC Security Update
And copy the whole folder c:\myapp from the building machine to the target machine.
It seems that from Vista, the 2005 redistributable package is included. So, no need to install it.
And voilà!