Home Download Gallery Documentation Developer

# Compilation with libQGLViewer

These instructions assume that you have installed Qt and libQGLViewer.
They detail how to compile a program that uses the libQGLViewer library. They assume you created a folder for your project (probably a copy of one of the examples), with your sources and a `myApp.pro` project file.

## Compilation on Windows

### Using Qt Creator

Open `myApp.pro` in Qt Creator then compile and run.

### Using `make`

Launch the Qt command prompt from the Qt folder in the Start menu. Then type:

    cd path\to\myApp
    qmake
    mingw32-make
    myApp

### Using Visual Studio

Launch Visual Studio from the Qt folder in the Start menu and then use the Qt menu to open the `.pro` file. All parameters should be properly configured and your program should compile.
With Visual Studio 6, use File-Open workspace to open the `.dsp` generated by `qmake` from the `.pro`.

### Providing `QGLViewer2.dll`

The `QGLViewer2.dll` (or `QGLViewer2d.dll` in debug mode) has to be found in order to execute your application.

- Copy it in a shared standard library directory such as `C:\Windows\System32`,
- or copy it in each application's directory,
- or add the path to the dlls using `set PATH=%PATH%;C:\Users\...\libQGLViewer-2.5.0\QGLViewer\release`

See also how to customize your .pro or the advices for libQGLViewer installation on Windows.

## Compilation on Mac

### Using Qt Creator

Open `myApp.pro` in Qt Creator then compile and run.

### Using Xcode

    cd path/to/myApp
    qmake -spec macx-xcode
    open myApp.xcodeproj

Then compile your project.

### Using `make`

    cd path/to/myApp
    qmake -spec macx-g++
    make
    open myApp.app

### Path customization

The standard installation will install the libQGLViewer framework in `~/Library/Frameworks`, where it can be accessed from any application.

Use `install_name` and read Apple's "Creating a Framework" as well as Qt's "Deploying an Application on Mac OS X" if you customize your path.

Use the `LIBS *= -L/path/to/lib -lQGLViewer` Unix syntax if you compiled a dylib instead of a framework.
See also how to customize your .pro or the libQGLViewer on Mac installation page.

## Compilation on Linux

### Using Qt Creator

Open `myApp.pro` in Qt Creator then compile and run.

### Using `make`

    cd path/to/myApp
    qmake
    make
    ./myApp

libQGLViewer uses the `-Wl,-rpath` syntax so that your executable finds the library. In case you are prompted with an `error while loading shared libraries` message when executing the program, fix that path. An other option is to copy `libQGLViewer.so` (created when you compiled the QGLViewer library) to your app's directory, or (better) move it to a standard library directory (such as `/usr/lib`, as is done when you `make install`).
Some configurations may require you to add `LIBS *= -lXi` in the `.pro` file. Debian users may have to replace `-lQGLViewer` by `-lQGLViewer-2` in their `.pro`.

See also how to customize your .pro or the libQGLViewer on Unix installation page.

## Customize the `.pro` for your application

Qt uses a `.pro` file to tune the compilation settings. The one that comes with the examples is pretty involved since it tries to automatically detect where the library and headers files are located. There is no need to change it: the examples should compile out of the box.

However, you can create a simplified `.pro` for your own projects. You need to set these three important variables in order to use libQGLViewer:

- `INCLUDEPATH` which must indicates where to find the QGLViewer's include files. Since the `#include`s use a `QGLViewer/qglviewer.h` syntax, the `QGLViewer` suffix should be omitted in this path (if libQGLViewer was installed in the `/xxx/yyy/QGLViewer` directory, you will set this variable to `INCLUDEPATH *= /xxx/yyy`).
- `LIBS` which tells that your program links with libQGLViewer. Use `LIBS *= -L/path/to/lib -lQGLViewer2`, where `/path/to/lib` is the path to the libQGLViewer lib (named `libQGLViewer2.{so|a|dylib}` or `QGLViewer2.lib` depending on your architecture). Use `-framework QGLViewer` instead if you compiled a Framework instead of a `dylib` on Mac (default).
- `QT` which lists the required Qt modules: `QT *= opengl xml` is a minimum.

Here is a simple although complete `.pro` (change the paths according to your configuration):

    TARGET = myViewer
    CONFIG *= qt opengl release
    QT *= opengl xml

    HEADERS = myViewer.h
    SOURCES = myViewer.cpp main.cpp

    # Windows
    INCLUDEPATH *= C:/Users/login/Documents/libQGLViewer-2.5.0
    LIBS *= -LC:/Users/login/Documents/libQGLViewer-2.5.0/QGLViewer -lQGLViewer2

    # Linux
    INCLUDEPATH *= /home/login/Documents/libQGLViewer-2.5.0
    LIBS *= -L/home/login/libQGLViewer-2.5.0/QGLViewer -lQGLViewer

    # Mac
    INCLUDEPATH *= /Users/login/Documents/libQGLViewer-2.5.0
    LIBS *= -F/Users/login/Library/Frameworks -framework QGLViewer

If you use Visual Studio, instead of editing the `.pro`, you can add the QGLViewer's headers path (without the trailing `QGLViewer`) to C/C++ / General / Additional Include Directories and add `QGLViewer2.lib` to the Linker / Input / Additional Dependencies using the project properties. These settings can also be shared by all your projects by setting the Tools / Options / Directories Include and Library values.
A `fatal error C1083: 'QDomElement' : No such file or directory` is fixed by adding `XML library` and `OpenGL library` in Project - Add Qt module.

## Going further

Now that your program compiles and runs, you can start improving it. You will probably start by changing the `draw()` method to define your own scene. Overload the `init()` function to initialize your scene as well as the OpenGL state (such as textures and objects). Read the principles of the library for details. If not already done, try out the different examples and feel free to cut and paste code for the functionalities you need.

## Localizing your application

See the Qt Linguist Manual for an explanation of the Qt translation model. Here are the lines you typically need to add in your `main` method to translate the texts of your interface.

      QApplication application(argc,argv);

      QString locale = QLocale::system().name();

      QTranslator translator;
      translator.load(QString("qglviewer_") + locale);
      // translator.load(your application specific translation file(s));
      app.installTranslator(&translator);

This assumes that `qglviewer_*.qm` is located in your application directory. If it is not the case, copy it or use the overloaded `load` method:

      translator.load(QString("qglviewer_") + locale, "C:\\Users\\login\\Documents\\Code\\libQGLViewer\\QGLViewer"); // for instance

QGLViewer translation files are available in the QGLViewer's header directory.

Please let me know if you encounter any problem following this guide.

Last modified on Thursday, December 19, 2013.