0

I'm currently learning how to use Qt. I want to try out some simple image processing applications using Qt, and since I'm already familiar with CImg I want to use that. I guess it should be possible to do so, if not mark my question for deletion or something.

My question is: how to get CImg working for Qt? CImg is a header file. Lets say its located on my desktop. I import it using Qt creator 4.1.0, by using the "add existing file..." in the rightclick menu on the header folder. Then my menu looks like this:

my project folder in qt.

It compiles when I add #include "CImg.h", but I can't use it, even when I'm trying to type using namespace cimg_library it will tell me that cimg_library doesn't exist. I also tried just creating a header file and copying the content of the CImg.h into it but then it simply fails to compile and the Qt Creator freezes.

Edit: I managed to make the situation a bit better by adding the header location to the include code (like this: #include "C:/Users/Marci/Desktop/CImg.h" )I can now "see" CImg related stuff in the dev environment, and it won't bother me with not finding the constructor for CImg or anything like that. However when I try to compile while using anything CImg related it will give me around 20 linker errors. (Error code: LNK2019) My .pro file looks like this:

#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------

 QT       += core gui

 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

 TARGET = grayscale
 TEMPLATE = app


SOURCES += main.cpp\
         mainwindow.cpp

HEADERS  += mainwindow.h \
             C:/Users/Marci/Desktop/CImg.h

LIBS += -C:/Users/Marci/Desktop/ -CImg.h

FORMS    += mainwindow.ui

Edit2: after implementing the changes that PeterT suggested in his comment my .pro file looks like this:

#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = grayscale
TEMPLATE = app


SOURCES += main.cpp\
         mainwindow.cpp

HEADERS  += mainwindow.h \


INCLUDEPATH += C:/Users/Marci/Desktop

FORMS    += mainwindow.ui

And my mainwindow.cpp (in which i'm trying to create a CImg object) looks like this:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <CImg.h>

using namespace cimg_library;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    CImg<unsigned char> myimage(100,200);
}

MainWindow::~MainWindow()
{
    delete ui;
}

The compiler errors i get are: error: C2871: 'cimg_library': a namespace with this name does not exist error: C2065: 'CImg': undeclared identifier error: C2062: type 'unsigned char' unexpected

I hope this is specific enough.

Á. Márton
  • 497
  • 1
  • 3
  • 21
  • 1
    Possible duplicate of [Adding external library into Qt Creator project](http://stackoverflow.com/questions/718447/adding-external-library-into-qt-creator-project) – hyde Nov 09 '16 at 16:44
  • Please comment on wether that suggested duplicate solves your issue. If it doesn't, then please clarify, for example adding the exact error message you get, and showing your .pro file – hyde Nov 09 '16 at 16:45
  • I don't really understand the notation in that referenced question. Even if it is a duplicate of mine I still don't understand it, so an answer is needed. My .pro file after trying to implement the stuff written in that question looks like this: http://pastebin.com/ZY7qwaK4. The error message I get when trying to type "using namespace cimg_library" is: error: C2871: 'cimg_library': a namespace with this name does not exist. – Á. Márton Nov 09 '16 at 17:26
  • @Á.Márton CImg is a header only library, you don't link against it. remove the `LIBS += -C:/Users/Marci/Desktop/ -CImg.h` and consider just using `INCLUDEPATH += C:/Users/Marci/Desktop` with a regular `#include ` – PeterT Nov 09 '16 at 21:27
  • I did what you suggested, I removed the LIBS thing and and the header access path from the HEADERS section. Added `INCLUDEPATH += C:/Users/Marci/Desktop`. I've also rewritten the `#include ` as you suggested. Now it refuses to recognize the CImg related code again. – Á. Márton Nov 09 '16 at 21:35
  • @Á.Márton can you actually include a minimal version of the the source-code that doesn't compile and the first error messages you get in the question. – PeterT Nov 10 '16 at 05:07
  • You need to link with `gdi32` on Windows - in your `LIBS` section. – Mark Setchell Jan 20 '17 at 21:08

1 Answers1

2

After I few months I figured it out. The problem lies in the fact, that CImg uses a windows .dll file for the visualizing functions of the class cimg_display. Since Qt is platform independent it doesnt like this. However you can make it work with an interesting trick. First you need to include the header file normally, in the project file. After that, whenever you actually #include it, you need to write the following macro: #define cimg_display 0 In my understanding this makes it work, because the C and C++ compilers simply copy the content of the included file into the source. And thanks to the macro the compiler will ignore the class thats causing trouble for us.

Á. Márton
  • 497
  • 1
  • 3
  • 21